【发布时间】:2017-08-03 21:55:24
【问题描述】:
我有一个包含 2 个变量的数据框:ID 和 outcome。我首先尝试groupbyID,然后计算outcome 在该ID 中的唯一值的数量。
df
ID outcome
1 yes
1 yes
1 yes
2 no
2 yes
2 no
预期输出:
ID yes no
1 3 0
2 1 2
我的代码df[['PID', 'outcome']].groupby('PID')['outcome'].nunique() 给出了唯一值本身的编号,这样:
ID
1 2
2 2
但是我需要yes 和no 的计数,我该如何实现呢?谢谢!
【问题讨论】:
标签: python pandas dataframe count unique