基于多列的pandas Gaggfunc?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (32)

数据如下所示:

inp = [{'Scenario':'Baseline', 'Region':'CHINA', 'Variable':'Methane', 'Unit':'MtCO2eq', '1990':5,'1995':10,'2000':15},
   {'Scenario':'Baseline', 'Region':'INDIA', 'Variable':'Methane', 'Unit':'MtCO2eq', '1990':6,'1995':11,'2000':16},
   {'Scenario':'Baseline', 'Region':'INDONESIA', 'Variable':'Methane', 'Unit':'MtCO2eq', '1990':7,'1995':12,'2000':17},
   {'Scenario':'Baseline', 'Region':'KOREA', 'Variable':'Methane', 'Unit':'MtCO2eq', '1990':8,'1995':13,'2000':18},
   {'Scenario':'Baseline', 'Region':'JAPAN', 'Variable':'Methane', 'Unit':'MtCO2eq', '1990':9,'1995':14,'2000':19},
   {'Scenario':'Baseline', 'Region':'THAILAND', 'Variable':'Methane', 'Unit':'MtCO2eq', '1990':10,'1995':15,'2000':20},
   {'Scenario':'Baseline', 'Region':'RUSSIA', 'Variable':'Methane', 'Unit':'MtCO2eq', '1990':11,'1995':16,'2000':21}]
dt = pd.DataFrame(inp)

dt 
   1990  1995  2000     Region  Scenario     Unit Variable
0     5    10    15      CHINA  Baseline  MtCO2eq  Methane
1     6    11    16      INDIA  Baseline  MtCO2eq  Methane
2     7    12    17  INDONESIA  Baseline  MtCO2eq  Methane
3     8    13    18      KOREA  Baseline  MtCO2eq  Methane
4     9    14    19      JAPAN  Baseline  MtCO2eq  Methane
5    10    15    20   THAILAND  Baseline  MtCO2eq  Methane
6    11    16    21     RUSSIA  Baseline  MtCO2eq  Methane

我运行这段代码:

dt_test = dt.pivot_table(dt,index=['Scenario','Variable','Unit'],
                           columns=[(df['Region'] == 'CHINA')|
                                   (df['Region'] == 'INDIA')|
                                   (df['Region'] == 'INDONESIA')
                                   |(df['Region'] == 'KOREA')],
                           aggfunc=np.sum)

并将其作为输出:

                           1990        1995        2000      
Region                    False True  False True  False True 
Scenario Variable Unit                                       
Baseline Methane  MtCO2eq    46    10    76    15   106    20
提问于
用户回答回答于

使用xs:

print (dt_test.xs(True, axis=1, level=1))
                           1990  1995  2000
Scenario Variable Unit                     
Baseline Methane  MtCO2eq    26    46    66

但最好是先过滤isinboolean indexing:

df = df[df['Region'].isin(['CHINA','INDIA','INDONESIA','KOREA'])]

print (df)
   1990  1995  2000     Region  Scenario     Unit Variable
0     5    10    15      CHINA  Baseline  MtCO2eq  Methane
1     6    11    16      INDIA  Baseline  MtCO2eq  Methane
2     7    12    17  INDONESIA  Baseline  MtCO2eq  Methane
3     8    13    18      KOREA  Baseline  MtCO2eq  Methane

然后聚合sum每组:

dt_test = df.groupby(['Scenario','Variable','Unit']).sum()
print (dt_test)
                           1990  1995  2000
Scenario Variable Unit                     
Baseline Methane  MtCO2eq    26    46    66

所属标签

可能回答问题的人

  • 嗨喽你好

    7 粉丝480 提问9 回答
  • 富有想象力的人

    3 粉丝0 提问7 回答
  • uncle_light

    5 粉丝518 提问6 回答
  • 人生的旅途

    10 粉丝484 提问6 回答

扫码关注云+社区

领取腾讯云代金券