how to aggregate difference in order by a columns

3376 views python
3

I have a panel dataset and the last column is the difference. I`d like to add up by Years , but grouped inside the country.

The original data is like these:

    CountryName  Years  d_ln_tfp
33     Australia   2011  0.1
34     Australia   2012  0.2
35     Australia   2013  0.3
36       Austria   2011  0.7
37       Austria   2012 -0.8
38       Austria   2013  0.3
39    Azerbaijan   2011  0.2
40    Azerbaijan   2012  0.7
41    Azerbaijan   2013  0.1

The ideal one is like these:

    CountryName  Years  ln_tfp
33     Australia   2011  0.1
34     Australia   2012  0.3 (<<0.2+0.1)
35     Australia   2013  0.6 (<<0.2+0.1+0.3)
36       Austria   2011  0.7
37       Austria   2012 -0.1 (<<0.7-0.8)
38       Austria   2013  0.2 (<<0.7-0.8+0.3)
39    Azerbaijan   2011  0.2
40    Azerbaijan   2012  0.9 (<<0.2+0.7)
41    Azerbaijan   2013  1.0 (<<0.2+0.7+0.1)

I have a little trouble in describing the question clearly so any better revision for the title is welcomed.

Thanks!

answered question

1 Answer

0

Use groupby + cumsum:

df.groupby('CountryName')['d_ln_tfp'].cumsum()

posted this

Have an answer?

JD

Please login first before posting an answer.