假设我有一个类似于下面的DataFrame:
term score
0 this 0
1 that 1
2 the other 3
3 something 2
4 anything 1
5 the other 2
6 that 2
7 this 0
8 something 1
我该如何按照term
列中的唯一值计算score
列中的实例数量?生成类似以下结果:
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
我已经阅读过的相关问题包括Python Pandas counting and summing specific conditions和COUNTIF in pandas python over multiple columns with multiple conditions,但似乎都不完全符合我的要求。如this question所提到的pivot_table
可能是相关的,但我缺乏经验并且pandas文档简洁难懂。谢谢任何建议。