答案 0 :(得分:0)
假设源DataFrame如下:
Id Status
0 747 good
1 587 bad
2 347 good
3 709 good
我认为 pivot 是一个错误的选择。 要计数个总值,更自然的解决方案是 value_counts 。 连同设置适当的列名,代码可以是:
res = df.Status.value_counts().reset_index()
res.columns = ['Status', 'total']
到目前为止,我们只有总数。要计算百分比,另一条指令 是必需的:
res['percentage'] = res.total / res.total.sum()
对于我的数据,结果是:
Status total percentage
0 good 3 0.75
1 bad 1 0.25