Last active
March 27, 2021 14:50
-
-
Save ryanorsinger/db1535dc21b82bb989c71fe27304f73c to your computer and use it in GitHub Desktop.
category_percentages_by_another_category visual
This file contains hidden or 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
# Following up on https://github.com/mwaskom/seaborn/issues/1027 and https://stackoverflow.com/questions/34615854/seaborn-countplot-with-normalized-y-axis-per-group | |
import pandas as pd | |
import seaborn as sns | |
def category_percentages_by_another_category(df, category_a, category_b): | |
(df.groupby(category_b)[category_a].value_counts(normalize=True) | |
.rename('percent') | |
.reset_index() | |
.pipe((sns.catplot, 'data'), x=category_a, y='percent', hue=category_b, kind='bar')) | |
def category_percentages_by_another_category_col(df, category_a, category_b): | |
""" | |
Produces a .catplot with a norma | |
""" | |
(df.groupby(category_b)[category_a].value_counts(normalize=True) | |
.rename('percent') | |
.reset_index() | |
.pipe((sns.catplot, 'data'), x=category_a, y='percent', col=category_b, kind='bar')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment