如何在pandas中创建新列,该列是基于条件的另一列的总和?

时间:2019-07-31 20:46:48

标签: python-3.x pandas numpy

我正在尝试将结果列作为数据框中所有行的值列之和,其中国家/地区等于该行中的国家/地区,并且日期在该行中的日期或该日期之前

Date        Country ValueResult
01/01/2019  France  10  10
03/01/2019  England 9   9
03/01/2019  Germany 7   7
22/01/2019  Italy   2   2
07/02/2019  Germany 10  17
17/02/2019  England 6   15
25/02/2019  England 5   20
07/03/2019  France  3   13
17/03/2019  England 3   23
27/03/2019  Germany 3   20
15/04/2019  France  6   19
04/05/2019  England 3   26
07/05/2019  Germany 5   25
21/05/2019  Italy   5   7
05/06/2019  Germany 8   33
21/06/2019  England 3   29
24/06/2019  England 7   36
14/07/2019  France  1   20
16/07/2019  England 5   41
30/07/2019  Germany 6   39
18/08/2019  France  6   26
04/09/2019  England 3   44
08/09/2019  Germany 9   48
15/09/2019  Italy   7   14
05/10/2019  Germany 2   50

我尝试了下面的代码,但总结了整列

df['result'] = df.loc[(df['Country'] == df['Country']) & (df['Date'] >= df['Date']), 'Value'].sum()

1 个答案:

答案 0 :(得分:0)

按照日期排序,您可以这样做:

df['Result'] = df.grouby('Coutry').Value.cumsum()