Can anyone help me to create a pandas pivot table to get the below ouput
please find the data frame
Use the following code:
result = pd.crosstab(index=df['Product line'], columns=df.City,
values=df['Stocked Unit'], aggfunc=np.sum, normalize='columns')\
.applymap(lambda x: f'{100 * x:.2f}%').rename_axis(
columns={'City': None}, index={'Product line': 'Row Labels'})
Important points:
normalize='columns'
- Sum of values in each column is to be 1 (100%).applymap(...)
- Convert fractions to percents (with 2 decimal digits).rename_axis(...)
- Clear the name of column index and rename the row index.To see the effect of each of the above points, run the above code without further points.
To get percentage of Consumed Unit to Stocked Unit you have to do it stepwise:
For each combination (group) of City and Product line compute the ratio (sum of Consumed Unit / sum of Stocked Unit):
wrk = df.groupby(['City', 'Product line']).apply(lambda grp:
grp['Consumed Unit'].sum() / grp['Stocked Unit'].sum())\
.rename('Ratio').reset_index()
Compute the result as a crosstab from the above DataFrame:
result = pd.crosstab(index=wrk['Product line'], columns=wrk.City, values=wrk.Ratio,
aggfunc=np.sum, normalize='columns').applymap(lambda x: f'{100 * x:.2f}%')\
.rename_axis(columns={'City': None}, index={'Product line': 'Row Labels'})\
.reset_index()
For your source data (from your recent comment) the result is:
Row Labels Mandalay Naypyitaw Yangon
0 Electronic accessories 30.00% 59.89% 28.65%
1 Fashion accessories 30.00% 0.00% 0.00%
2 Food and beverages 40.00% 0.00% 0.00%
3 Health and beauty 0.00% 0.00% 25.55%
4 Home and lifestyle 0.00% 40.11% 22.90%
5 Sports and travel 0.00% 0.00% 22.90%