I have a data frame:
import pandas as pd
import numpy as np
df=pd.DataFrame.from_items([('STAND_ID',[1,1,2,3,3,3]),('Species',['Conifer','Broadleaves','Conifer','Broadleaves','Conifer','Conifer']),
('Height',[20,19,13,24,25,18]),('Stems',[1500,2000,1000,1200,1700,1000]),('Volume',[200,100,300,50,100,10])])
STAND_ID Species Height Stems Volume
0 1 Conifer 20 1500 200
1 1 Broadleaves 19 2000 100
2 2 Conifer 13 1000 300
3 3 Broadleaves 24 1200 50
4 3 Conifer 25 1700 100
5 3 Conifer 18 1000 10
I want to group by STAND_ID and Species, apply a weighted mean on Height and Stems with Volume as weight and unstack.
So i try:
newdf=df.groupby(['STAND_ID','Species']).agg({'Height':lambda x: np.average(x['Height'],weights=x['Volume']),
'Stems':lambda x: np.average(x['Stems'],weights=x['Volume'])}).unstack()
Which give me error:
builtins.KeyError: 'Height'
How can i fix this?
Your error is because you can not do multiple series/column operations using agg
. Agg takes one series/column as a time. Let's use apply
and pd.concat
.
g = df.groupby(['STAND_ID','Species'])
newdf = pd.concat([g.apply(lambda x: np.average(x['Height'],weights=x['Volume'])),
g.apply(lambda x: np.average(x['Stems'],weights=x['Volume']))],
axis=1, keys=['Height','Stems']).unstack()
g = df.groupby(['STAND_ID','Species'])
newdf = g.apply(lambda x: pd.Series([np.average(x['Height'], weights=x['Volume']),
np.average(x['Stems'],weights=x['Volume'])],
index=['Height','Stems'])).unstack()
Output:
Height Stems
Species Broadleaves Conifer Broadleaves Conifer
STAND_ID
1 19.0 20.000000 2000.0 1500.000000
2 NaN 13.000000 NaN 1000.000000
3 24.0 24.363636 1200.0 1636.363636
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