gpt4 book ai didi

python - 具有零值的 Pandas 汇总表

转载 作者:行者123 更新时间:2023-12-04 00:54:25 26 4
gpt4 key购买 nike

我正在尝试使用来自 python 的 pandas 的 .describe() 创建一个汇总表。

我有以下数据框:

df = pd.DataFrame({'Group':['Group1', 'Group1', 'Group1', 'Group2', 'Group2', 'Group2', 'Group3', 'Group3', 'Group4'],
'Cat':['Cat1', 'Cat2', 'Cat3', 'Cat4', 'Cat5', 'Cat', 'Cat7', 'Cat8', 'Cat9'],
'Value':[1230,4019,9491,9588,6402,1923,492,8589,8582]})
df

Group Cat Value
0 Group1 Cat1 1230
1 Group1 Cat2 4019
2 Group1 Cat3 9491
3 Group2 Cat4 9588
4 Group2 Cat5 6402
5 Group2 Cat 1923
6 Group3 Cat7 492
7 Group3 Cat8 8589
8 Group4 Cat9 8582

我想生成一个按 Group 和 Cat 分组的汇总表,所有不在 Group 中的 Cat 都以相同的方式出现,所有值 = 0。

我正在尝试:

        df.groupby(['Group', 'Cat']).describe()

# That has the following output:
Value
count mean std min 25% 50% 75% max
Group Cat
Group1 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
Group2 Cat 1.0 1923.0 NaN 1923.0 1923.0 1923.0 1923.0 1923.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
Group3 Cat7 1.0 492.0 NaN 492.0 492.0 492.0 492.0 492.0
Cat8 1.0 8589.0 NaN 8589.0 8589.0 8589.0 8589.0 8589.0
Group4 Cat9 1.0 8582.0 NaN 8582.0 8582.0 8582.0 8582.0 8582.0

但我想要的输出是:

                Value
count mean std min 25% 50% 75% max
Group Cat
Group1 Cat1 1.0 1230.0 NaN 1230.0 1230.0 1230.0 1230.0 1230.0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.0 0.0 0.0 0.0 0.0
Group4 Cat1 0.0 0.0 NaN 0.0 0.0 0.0 0.0 0.0
Cat2 0.0 0.0 NaN 0.0 0.0 0.0 0.0 0.0
Cat3 0.0 0.0 NaN 0.0 0.0 0.0 0.0 0.0
Cat4 0.0 0.0 NaN 0.0 0.0 0.0 0.0 0.0
Cat5 0.0 0.0 NaN 0.0 0.0 0.0 0.0 0.0
Cat6 0.0 0.0 NaN 0.0 0.0 0.0 0.0 0.0
Cat7 0.0 0.0 NaN 0.0 0.0 0.0 0.0 0.0
Cat8 0.0 0.0 NaN 0.0 0.0 0.0 0.0 0.0
Cat9 1.0 8582.0 NaN 8582.0 8582.0 8582.0 8582.0 8582.0

我想知道如何得到这个输出。

最佳答案

你也可以根据你得到的索引和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
....................................
...............................

关于python - 具有零值的 Pandas 汇总表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63764972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com