【发布时间】:2020-11-25 14:01:40
【问题描述】:
如何从购买的商品中删除最受欢迎的商品?
我想从我的数据框中删除最受欢迎的文章。
我已经收到前 5 篇文章。不幸的是,我不知道如何从我的个人购买中删除这些。
例如,购买包含一件热门商品和两件常规商品。清理完成后,热门项目应被移除,只剩下两个项目。
import pandas as pd
d = {'purchaseid': [0, 0, 0, 1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 9, 9, 9, 9],
'itemid': [ 3, 8, 2, 10, 3, 10, 4, 12, 3, 12, 3, 4, 8, 6, 3, 0, 5, 12, 9, 9, 13, 1, 7, 11, 11]}
df = pd.DataFrame(data=d)
print(df.head(5))
print(df['itemid'].nunique())
gb = df.groupby("itemid").size().nlargest(n=5, keep="first")
print(gb) # gives me the popularst items with the count
purchaseid itemid
0 0 3
1 0 8
2 0 2
3 1 10
4 2 3
14
# the popularst items
itemid
3 5
12 3
4 2
8 2
9 2
清理后我想要什么
purchaseid itemid
- - - # purchase 0 has only 2 instead of 3 purchases
0 0 8
1 0 2
2 1 10
- - - # This line should be completely removed, this is only for illustration.
【问题讨论】: