2 回答
TA贡献1868条经验 获得超4个赞
您还可以根据获得的索引创建笛卡尔积索引列表reindex:
out = df.groupby(['Group', 'Cat']).describe()
idx = pd.MultiIndex.from_product((out.index.levels[0],out.index.levels[1]))
out = out.reindex(idx,fill_value=0)
Value
count mean std min 25% 50% 75% max
Group1 Cat 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Cat1 1.0 1230.0 NaN 1230.0 1230.0 1230.0 1230.0 1230.0
Cat2 1.0 4019.0 NaN 4019.0 4019.0 4019.0 4019.0 4019.0
Cat3 1.0 9491.0 NaN 9491.0 9491.0 9491.0 9491.0 9491.0
Cat4 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Cat5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Cat7 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Cat8 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Cat9 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Group2 Cat 1.0 1923.0 NaN 1923.0 1923.0 1923.0 1923.0 1923.0
Cat1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Cat2 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Cat3 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Cat4 1.0 9588.0 NaN 9588.0 9588.0 9588.0 9588.0 9588.0
Cat5 1.0 6402.0 NaN 6402.0 6402.0 6402.0 6402.0 6402.0
Cat7 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Cat8 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Cat9 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Group3 Cat 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Cat1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
....................................
...............................
TA贡献1840条经验 获得超5个赞
检查unstack
+ stack
,注意我还建议保留行值NaN
不填充 0
out = df.groupby(['Group', 'Cat']).describe().unstack().stack(dropna=False)
添加回答
举报