python读取特定两列_如何基于python中的where函数获取两列值

问题是:

基于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 the rating and product_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

the product_id value and rating 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 Library为此编写代码,但无济于事.这就是我所做的全部,但未输出我想要的.

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))

请帮帮我..谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值