第一只过滤前5名产品
boolean indexing
具有
isin
size
并通过
unstack
reindex
为正确排列列和最后一列,请使用
f-strings
list comprehension
:
#top5 created dynamically from column Product_id
#top5 = df['Product_id'].value_counts().index[:5])
top5 = ['P112','P134','P219','P361','P443']
df = (df[df['Product_id'].isin(top5)]
.groupby(['Employee_id','Product_id'])
.size()
.unstack(fill_value=0)
.reindex(columns=top5, fill_value=0))
df.columns = [f'Top{x}' for x in range(1,6)]
print (df)
Top1 Top2 Top3 Top4 Top5
Employee_id
E12 1 0 0 0 0
E19 0 0 1 0 0
E20 0 0 1 0 0
df = df.reset_index()
print (df)
Employee_id Top1 Top2 Top3 Top4 Top5
0 E12 1 0 0 0 0
1 E19 0 0 1 0 0
2 E20 0 0 1 0 0