[英]pandas groupby when count is zero and how to include zero value in result
Please consider any solution without usage of pivot_table() or unstack()请考虑无pivot_table()或拆散使用任何解决方案()
For the following dataframe:对于以下数据框:
import pandas as pd
df = pd.DataFrame({
'name': ['Mason', 'Ali', 'Si', 'Pedram'],
'continent': ['Europe', 'Asia', 'Asia', 'Asia'],
'blood_type': ['AB', 'O+', 'AB', 'AB']
})
Which is:这是:
name continent blood_type
0 Mason Europe AB
1 Ali Asia O+
2 Si Asia AB
3 Pedram Asia AB
The following groupby count:以下 groupby 计数:
df.groupby(['continent', 'blood_type']).count()
Will produce:将产生:
name
continent blood_type
Asia AB 2
O+ 1
Europe AB 1
Instead, how can I include zero value count like the table below?相反,我如何包含如下表所示的零值计数? (by not using pivot_table or unstack )
(通过不使用pivot_table或拆散)
name
continent blood_type
Asia AB 2
O+ 1
Europe AB 1
O+ 0
As of pandas
0.25 (or 0.24.2, not sure now), if you groupby a Category ( pd.Categorical
), it will show all values in the final count.从
pandas
0.25(或0.24.2,现在不确定)开始,如果您按类别( pd.Categorical
) pd.Categorical
,它将在最终计数中显示所有值。
df.groupby([pd.Categorical(df.continent), 'blood_type']).count().fillna(0)
name continent
blood_type
Asia AB 2.0 2.0
O+ 1.0 1.0
Europe AB 1.0 1.0
O+ 0.0 0.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.