Created
June 17, 2023 20:42
-
-
Save konverner/35be564bc6fb87687d7bcaba43ddfc6a to your computer and use it in GitHub Desktop.
stacked barplot from dataframe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
df = pd.DataFrame({'name': ['Store A', 'Store B', 'Store C', 'Store D'] | |
'district': ['I', "II", "I", "III"], | |
"category": [X, X, Y, Z] | |
} | |
) | |
district_category_pivot_table = df.pivot_table('name', 'district', 'category', aggfunc='count') | |
district_category_pivot_table['total'] = district_category_pivot_table.sum(1) | |
district_category_pivot_table\ | |
.sort_values(by='total', ascending=True)\ | |
.drop(columns=['total'])\ | |
.plot.barh(stacked=True, xlabel='', ylabel='', grid=True, | |
title='Number of name by district and category'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment