groupby的累积总和

时间:2016-05-07 10:29:23

标签: pandas

我有一些像这样的数据

Manufacture,Country,side,Quantity
Toyota,US,B,10
Toyota,CN,S,5
Honda,US,B,10
Honda,US,S,10
Honda,US,S,5
Ford,CN,B,8
Ford,US,S,3
Ford,CN,B,2

我想得到买入国(B)和卖出国(S)的累积总和

类似的东西,

Manufacture,Country,B,S,countrybuy,countrysell
Toyota,US,10,0,10,0
Toyota,CN,0,5,0,5
Honda,US,10,0,20,0
Honda,US,0,10,20,10
Honda,US,0,5,20,15
Ford,CN,8,0,8,5
Ford,US,0,3,20,18
Ford,CN,2,0,10,5

1 个答案:

答案 0 :(得分:2)

<强>更新

首先我们可以像这样转移数据:

In [45]: pvt = (df.pivot_table(index=['Manufacture','Country'], columns='side',
   ....:                       values='Quantity', fill_value=0, aggfunc='sum')
   ....:          .reset_index()
   ....:       )

In [46]: pvt
Out[46]:
side Manufacture Country   B   S
0           Ford      CN  10   0
1           Ford      US   0   3
2          Honda      US  10  15
3         Toyota      CN   0   5
4         Toyota      US  10   0

现在我们可以计算cumsum

In [47]: pvt[['countrybuy','countrysell']] = pvt.groupby('Country')['B','S'].cumsum()

In [48]: pvt
Out[48]:
side Manufacture Country   B   S  countrybuy  countrysell
0           Ford      CN  10   0          10            0
1           Ford      US   0   3           0            3
2          Honda      US  10  15          10           18
3         Toyota      CN   0   5          10            5
4         Toyota      US  10   0          20           18

OLD回答:

In [222]: df[['countrybuy','countrysell']] = df.groupby('Country')['B','S'].cumsum()

In [223]: df
Out[223]:
  Manufacture Country   B   S  countrybuy  countrysell
0      Toyota      US  10   0          10            0
1      Toyota      CN   0   5           0            5
2       Honda      US  10   0          20            0
3       Honda      US   0  10          20           10
4       Honda      US   0   5          20           15
5        Ford      CN   8   0           8            5
6        Ford      US   0   3          20           18
7        Ford      CN   2   0          10            5