我得到了以下熊猫数据框:
Id Category
1 type 2
1 type 3
1 type 2
2 type 1
2 type 2
我需要处理上述数据帧并将其转置为:
Id Category_type_1 Category_type_2 Category_type_3
1 0 2 1
2 1 1 0
如果有人能展示用 python 编写此代码的最简单方法,我将不胜感激。
I'd groupby
并使用size
df.groupby(df.columns.tolist()).size().unstack().fillna(0)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)