Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryanorsinger/db1535dc21b82bb989c71fe27304f73c to your computer and use it in GitHub Desktop.
Save ryanorsinger/db1535dc21b82bb989c71fe27304f73c to your computer and use it in GitHub Desktop.
category_percentages_by_another_category visual
# 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