问题是:
基于user_id
列,我想得到rating
和{rating
和product_id
列值的所有用户记录,但对于用户未对电影进行评级的电影,应将其显示为Nan,但仍应检索product_id
。下表提供了一些数据。在
| product_id | user_id | user_name | rating |
|-------------|-----------------|----------------------------------------------|--------|
| B0009XRZ92 | A2JFZLAUG3YFQ7 | Entropy Babe "EB" | 5 |
| B0009XRZ92 | A22HGAAO8KZ2N3 | R. Metzelar | 5 |
| B000067A8B | A2NJO6YE954DBH | Lawrance M. Bernabo | 4 |
| B0009XRZ92 | A3HE4MYMWK4AER | Rebecca M. Eddy "Foster Mom and Untbunny" | 5 |
| B003A3R3ZY | A9A2PR663ED1V | Roger D. Goff | 5 |
| B0009XRZ92 | A2MRZDJF90JC1U | Suzanne K. Armstrong "Suzy Q" | 5 |
| B0009XRZ92 | A2YNBDT3170PCR | C. O'Hern | 5 |
| B0009XRZ92 | A10VJ7BDVCPKEZ | Carol S. Bottom | 5 |
| B0009XRZ92 | AAAQO894MG80B | Paul J. Michko | 5 |
| B00067BBQE | A9A2PR663ED1V | Roger D. Goff | 5 |
| B0009XRZ92 | A31S5QUMFR8NH2 | Dana L. Jordan "Mom of Twins" | 5 |
| B0009XRZ92 | A2DS24DHXUH0GM | Gaz Rev(iewer) | 4 |
| B00006AUMZ | A2NJO6YE954DBH | Lawrance M. Bernabo | 4 |
| B0009XRZ92 | A16FRHL2ZC7EUR | M. Claytor | 5 |
| B0009XRZ92 | A3AV8R0A62PP1N | MARCUSHELBLINZ "mmmacman" | 5 |
| B0009XRZ92 | A3QN84C38DE9FU | Gillian M. Kratzer | 5 |
| B0009XRZ92 | A36MLTLVQFEQYL | Yossarian "alienated socialist" | 5 |
| B00006AUMD | A2NJO6YE954DBH | Lawrance M. Bernabo | 4 |
What I want to do is:
To take one
user_id
at a time and display therating
andproduct_id
columns value for that user for all the movies in the table and if the user didn't rate some movies then the record should be displayed with theproduct_id
value andrating
as Nan and the whole process should be repeated for all the users.
例如,user_id: A2NJO6YE954DBH
的记录如下所示:
我试着用Pandas库来编写代码,但还是忍不住。这是我所做的一切,但它没有输出我想要的。在
import pandas as pd
df =pd.read_csv('out.csv')
unique_users=df.user_id.unique()
for x, y in enumerate(unique_users):
print(df[['rating','product_id']].where(df.user_id==y))
请帮帮我。。谢谢
如果我理解正确,您可以在此处使用^{} :
试试看
您可以提取
user_id
等于特定值的行,方法是:相关问题 更多 >
编程相关推荐