按两列分组并求和?

Group by two columns and get sum?

x1 = [{'id1': 'Africa', 'id2': 'Europe', 'v': 1}, 
      {'id1': 'Europe', 'id2': 'North America', 'v': 5},
      {'id1': 'North America', 'id2': 'Asia', 'v': 2,}, 
      {'id1': 'North America', 'id2': 'Asia', 'v': 3}]

df = pd.DataFrame(x1)

我如何按大洲分组并根据第 'v' 列获得总和?

例如,我希望获得每个大陆的值总和,如下所示:

Africa: 1 (1)
Europe: 6 (1 + 5)
North America: 10 (5 + 2 + 3)
Europe: 6 (1 + 5)

使用 melt 并聚合 sum:

s = df.melt('v').groupby('value')['v'].sum()
print (s)
value
Africa            1
Asia              5
Europe            6
North America    10
Name: v, dtype: int64

对于DataFrame

df = df.melt('v', value_name='a').groupby('a', as_index=False)['v'].sum()
print (df)
               a   v
0         Africa   1
1           Asia   5
2         Europe   6
3  North America  10

按每列分组,然后相加结果:

df.groupby('id1').sum().add(df.groupby('id2').sum(), fill_value=0).astype(int)
#                v
#Africa          1
#Asia            5
#Europe          6
#North America  10