作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题是:基于 user_id
列,我想获取 rating
和 product_id
列的值。可以有多个条目具有相同的 user_id。我想用 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
的记录将如下所示:
| product_id | rating |
|------------|--------|
| B000067A8B | 4 |
| B00006AUMD | 4 |
| B00006AUMD | 4 |
| B0009XRZ92 | Nan |
| B003A3R3ZY | Nan |
| B00067BBQE | Nan |
| . | . |
| . | . |
| . | . |
我曾尝试使用 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))
请帮帮我..谢谢
最佳答案
如果我没理解错的话,你可以使用pd.pivot_table()
这里:
df_new=df.pivot_table(index='user_id',columns='product_id',values='rating').rename_axis(None,1)
print(df_new)
B000067A8B B00006AUMD B00006AUMZ B00067BBQE \
user_id
A10VJ7BDVCPKEZ NaN NaN NaN NaN
A16FRHL2ZC7EUR NaN NaN NaN NaN
A2DS24DHXUH0GM NaN NaN NaN NaN
A2NJO6YE954DBH 4.0 4.0 4.0 NaN
A2YNBDT3170PCR NaN NaN NaN NaN
A36MLTLVQFEQYL NaN NaN NaN NaN
A3HE4MYMWK4AER NaN NaN NaN NaN
A3QN84C38DE9FU NaN NaN NaN NaN
AAAQO894MG80B NaN NaN NaN NaN
A22HGAAO8KZ2N3 NaN NaN NaN NaN
A2JFZLAUG3YFQ7 NaN NaN NaN NaN
A2MRZDJF90JC1U NaN NaN NaN NaN
A31S5QUMFR8NH2 NaN NaN NaN NaN
A3AV8R0A62PP1N NaN NaN NaN NaN
A9A2PR663ED1V NaN NaN NaN 5.0
B0009XRZ92 B003A3R3ZY
user_id
A10VJ7BDVCPKEZ 5.0 NaN
A16FRHL2ZC7EUR 5.0 NaN
A2DS24DHXUH0GM 4.0 NaN
A2NJO6YE954DBH NaN NaN
A2YNBDT3170PCR 5.0 NaN
A36MLTLVQFEQYL 5.0 NaN
A3HE4MYMWK4AER 5.0 NaN
A3QN84C38DE9FU 5.0 NaN
AAAQO894MG80B 5.0 NaN
A22HGAAO8KZ2N3 5.0 NaN
A2JFZLAUG3YFQ7 5.0 NaN
A2MRZDJF90JC1U 5.0 NaN
A31S5QUMFR8NH2 5.0 NaN
A3AV8R0A62PP1N 5.0 NaN
A9A2PR663ED1V NaN 5.0
关于python - 如何根据python中的Where函数获取两列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54808927/
我是一名优秀的程序员,十分优秀!