Normalize Data And Plot As Stacked Bar Plot With Python/ Pandas
I have the following data frame: land_cover canopy_cat count tc_density_cor 0 20 1 56 35.760967 1 20 2 28 35.7609
Solution 1:
I think you need:
df['Count Per Canopy Cat'] = (df['count'] * df['tc_density_cor'] /
df.groupby('land_cover')['count'].transform(sum))
df.pivot('land_cover',
'canopy_cat',
'Count Per Canopy Cat')\
.plot.bar(stacked=True, figsize=(15,8))
Chart:
Solution 2:
IIUC
d = df.set_index(
['land_cover', 'canopy_cat']
).pipe(
lambda d: d['count'].div(d['count'].sum(level=0), axis=0, level=0) * d['tc_density_cor']
).unstack()
d.iloc[:, :5]
canopy_cat 12345
land_cover
2015.7686157.8843083.0974072.5342421.126330307.2784543.2265312.0634791.3506410.787874407.9084533.3573621.4175531.0445130.671472
d.plot.bar(stacked=True)
Same answer refactored
def normalize(d):
sums = d['count'].sum(level='land_cover')
return d['count'].div(sums, axis=0, level='land_cover') * d['tc_density_cor']
d = df.set_index(['land_cover', 'canopy_cat']).pipe(normalize).unstack()
d.plot.bar(stacked=True)
Post a Comment for "Normalize Data And Plot As Stacked Bar Plot With Python/ Pandas"