1 回答
TA贡献1810条经验 获得超5个赞
您可以groupby查找M2每个类别的数量,并将其作为列添加到您的数据框中,如下所示
df['count_M2'] = df.groupby('M2')['M1'].transform('count')
然后运行该pivot_table函数以获取M1每组中M2的百分比
df.pivot_table(values=['count_M2'], index=['M1'], columns=['M2'],
aggfunc=lambda x: len(x) / x.iloc[0])
细节
df
Time M1 M2 M3 M4 count_M2
0 2018-09-20 12:59:57 cat 1 obj_1 name_1 1 1
1 2018-09-20 12:58:53 cat 1 obj_2 name_2 1 3
2 2018-09-20 12:57:44 else 1 obj_3 name_1 1 1
3 2018-09-20 12:57:19 cat 11 obj_2 name_1 1 3
4 2018-09-20 12:56:17 cat 11 obj_2 name_1 1 3
df.pivot_table
count_M2
M2 obj_1 obj_2 obj_3
M1
cat 1 1.0 0.333333 NaN
cat 11 NaN 0.666667 NaN
else 1 NaN NaN 1.0
添加回答
举报