5

I have a dataframe that contains values for countries and years:

country year value
US       2000  20
JP       2000  10
AU       2000   5
US       2001  22
JP       2001  12
AU       2001   6
US       2002  23
JP       2002  14
AU       2002   8

I want to calculate the percent change for each country between years, so I group by countries and iterate per group:

grouped=df.groupBy('country')
for group in grouped:
    group['pct']=group['value'].pct_change(periods=1)*100

How can I create a new dataframe from 'grouped' containing my new column pct?

0

1 Answer 1

7

Just put your code in a function and use apply:

def f(group):
    group['pct']=group['value'].pct_change(periods=1)*100
    return group    
print df.groupby('country').apply(f)

Output:

  country  year  value        pct
0      US  2000     20        NaN
1      JP  2000     10        NaN
2      AU  2000      5        NaN
3      US  2001     22  10.000000
4      JP  2001     12  20.000000
5      AU  2001      6  20.000000
6      US  2002     23   4.545455
7      JP  2002     14  16.666667
8      AU  2002      8  33.333333
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.