Reputation: 792
I have a dataframe with the following columns:
gender class
male A
female A
male B
female B
male B
female A
I want to plot a double bar graph with the columns as each gender and the values as the count of how many of each gender are in class A vs B respectively.
So the bars should be grouped by gender and there should be 2 bars - one for each class.
How do I visualize this? I see this example but I'm really confused
speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
index = ['snail', 'pig', 'elephant',
'rabbit', 'giraffe', 'coyote', 'horse']
df = pd.DataFrame({'speed': speed,
'lifespan': lifespan}, index=index)
speed lifespan
snail 0.1 2.0
pig 17.5 8.0
elephant 40.0 70.0
rabbit 48.0 1.5
giraffe 52.0 25.0
coyote 69.0 12.0
horse 88.0 28.0
ax = df.plot.bar(rot=0)
My index is just row 0 to the # of rows, so I'm confused how I can configure df.plot.bar
to work with my use case. Any help would be appreciated!
Upvotes: 3
Views: 9345
Reputation: 62523
pandas.DataFrame.pivot_table
to reshape the dataframe from a long to wide format. The index will be the x-axis, and the columns will be the groups when plotted with pandas.DataFrame.plot
pd.crosstab(df['gender'], df['class'])
can also be used to reshape with an aggregation.seaborn.countplot
and hue='class'
, or the figure level version seaborn.catplot
with kind='count'
, both of which can create the desired plot without reshaping the dataframe.df.index
or reset the index with df = df.reset_index()
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = {'gender': ['male', 'female', 'male', 'female', 'male', 'female'], 'class': ['A', 'A', 'B', 'B', 'B', 'A']}
df = pd.DataFrame(data)
# pivot the data and aggregate
dfp = df.pivot_table(index='gender', columns='class', values='class', aggfunc='size')
# plot
dfp.plot(kind='bar', figsize=(5, 3), rot=0)
plt.show()
plt.figure(figsize=(5, 3))
sns.countplot(data=df, x='gender', hue='class')
plt.show()
sns.catplot(kind='count', data=df, x='gender', hue='class', height=3, aspect=1.4)
plt.show()
Upvotes: 3