【发布时间】:2022-01-19 21:23:48
【问题描述】:
对于下图所示的示例数据:
如果条件 customer_id 相同,我如何获得一列中出现的相似项目的数量?
ls=[]
for i in data['customer_id']:
sum=0
for j in data['category']:
if i == j[0]:
sum+=j[1]
ls.append(sum)
简而言之:
[food and fruit, vegetable, bakery and bread, cookies snacks or candies, seafoods and meat]
customer_id[0] = [4,9,5,1,0]
【问题讨论】:
-
欢迎来到 SO!请不要将数据或代码添加为图像。而是使用纯文本或更好的方法,提供一段构建数据框的代码。这将更有可能有人能够快速帮助您。
标签: python pandas list dataframe for-loop