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
?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With