Home > Software engineering >  Get and graph groupby result distribution of a column
Get and graph groupby result distribution of a column

Time:06-23

I want to graph my group's distribution of a label column. I was able to do so with creating dummies, crating pivot table of each of the groups, and then create a loop to build a new dataframe. I am looking for a shorter way. Maybe with more advance methods of groupby? And also I don't know how to create a side by side bar chart instead of the stack bar chart I have here.

To recreate the dataframe:

import pandas as pd
import numpy as np

np.random.seed(1)
a = np.random.choice(['region_A', 'region_B', 'region_C', 'region_D', 'region_E'], size=30, p= 
[0.1, 0.2, 0.3, 0.30, 0.1])
b = np.random.choice(['1', '0'], size=30, p=[0.5, 0.5])
df = pd.DataFrame({'region': a, 'label': b})

My desire graph:

dummy = pd.get_dummies(df['region'])
region_lst = []
label_0 = []
label_1 = []
for col in dummy.columns:
    region_lst.append(col)
    label_0.append(pd.crosstab(dummy[col], df['label']).iloc[1,0])
    label_1.append(pd.crosstab(dummy[col], df['label']).iloc[1,1])

df_labels = pd.DataFrame({'label_0': label_0, 'label_1': label_1}, index=region_lst)
df_labels.plot.bar()

CodePudding user response:

Use enter image description here

intermediate crosstab:

label     0  1
region        
region_A  2  3
region_B  3  3
region_C  5  4
region_D  3  6
region_E  1  0
  • Related