2 回答

TA贡献1794条经验 获得超8个赞
使用groupbywithsize和 reshape by unstack, last add_prefix:
df = df.groupby(['term','score']).size().unstack(fill_value=0).add_prefix('score ')
或使用crosstab:
df = pd.crosstab(df['term'],df['score']).add_prefix('score ')
或者pivot_table:
df = (df.pivot_table(index='term',columns='score', aggfunc='size', fill_value=0)
.add_prefix('score '))
print (df)
score score 0 score 1 score 2 score 3
term
anything 0 1 0 0
something 0 1 1 0
that 0 1 1 0
the other 0 0 1 1
this 2 0 0 0

TA贡献2065条经验 获得超14个赞
您还可以使用, get_dummies, set_index, 和sum带level参数:
(pd.get_dummies(df.set_index('term'), columns=['score'], prefix_sep=' ')
.sum(level=0)
.reset_index())
输出:
term score 0 score 1 score 2 score 3
0 this 2 0 0 0
1 that 0 1 1 0
2 the other 0 0 1 1
3 something 0 1 1 0
4 anything 0 1 0 0
添加回答
举报