I have the following dataframe:-
traffic_type date region total_views
desktop 01/04/2018 aug 50
mobileweb 01/04/2018 aug
One idea with pivoting:
df1 = df.pivot_table(index=['date','region'],
columns='traffic_type',
values='total_views',
aggfunc='sum')
print (df1)
traffic_type desktop mobileweb total
date region
01/04/2018 aug 50 60 200
world 20 30 40
df2 = df1['desktop'].div(df1['total']).reset_index(name='desktop_share').assign(traffic_type='total')
df = df.merge(df2, how='left')
print (df)
traffic_type date region total_views desktop_share
0 desktop 01/04/2018 aug 50 NaN
1 mobileweb 01/04/2018 aug 60 NaN
2 total 01/04/2018 aug 200 0.25
3 desktop 01/04/2018 world 20 NaN
4 mobileweb 01/04/2018 world 30 NaN
5 total 01/04/2018 world 40 0.50
Another idea with MultiIndex
:
df1 = df.set_index(['traffic_type','date','region'])
a = df1.xs('desktop', drop_level=False).rename({'desktop':'total'})
b = df1.xs('total', drop_level=False)
df = df1.assign(desktop_share = a['total_views'].div(b['total_views'])).reset_index()
print (df)
traffic_type date region total_views desktop_share
0 desktop 01/04/2018 aug 50 NaN
1 mobileweb 01/04/2018 aug 60 NaN
2 total 01/04/2018 aug 200 0.25
3 desktop 01/04/2018 world 20 NaN
4 mobileweb 01/04/2018 world 30 NaN
5 total 01/04/2018 world 40 0.50