I have a dataset as below, where Q1,Q2,Q3 are categorical.
Q1,Q2,Q3 0 4,1,5 1 1,5,1 2 1,2,1 3 1,4,1 4 5,1,5
How can I plot the x axis for each column, and y as the count of the value for each column, all in one plot.
Sample out put
Advertisement
Answer
You can use value_counts
on the columns and then plot:
# grouped by quartile df.apply(pd.Series.value_counts).T.plot.bar()
# grouped by value df.apply(pd.Series.value_counts).plot.bar()
old answer
A quick way using pandas only is:
df.groupby(level=0, axis=1).plot.bar()
But this won’t be flexible for the colors/layout.
If you want a nice output, rework your dataframe as long format and use seaborn.catplot
:
import seaborn as sns df2 = (df.unstack() .reset_index() .rename(columns={'level_0': 'quartile', 'level_1': 'index', 0: 'value'}) ) sns.catplot(data=df2, col='quartile', x='index', y='value', kind='bar')
I used the following input:
Q1 Q2 Q3 0 4 1 5 1 1 5 1 2 1 2 1 3 1 4 1 4 5 1 5
combining both answers:
import seaborn as sns df2 = (df.apply(pd.Series.value_counts) .unstack() .reset_index() .rename(columns={'level_0': 'quartile', 'level_1': 'index', 0: 'counts'}) ) sns.catplot(data=df2, col='quartile', x='index', y='counts', kind='bar')