I have a Pandas
dataframe that looks like the following.
year month class ---- ----- ----- 2015 1 1 2015 1 1 2015 1 2 2015 1 2 ...
I want to be able to create 2 bar chart series of of this data on one plot. If I can do a groupby
, count
and end up with a data frame
then I am thinking I can just do a simple dataframe.plot.barh
.
What I have tried is the following code.
x = df.groupby(['year', 'month', 'class'])['class'].count()
What x
ends up being is a Series
. So then I do the following to get a DataFrame
.
df = pd.DataFrame(x)
Which gets me pretty close. The data ends up looking like the following.
clazz year month clazz 2015 1 1 22 1 152 2 45
But when I do a bar plot df.plot.bar()
, I only see one series. The output desired is simply in one series, from 2015-01 to 2019-12, how many times did class
1 occur per month? And then another series, from 2015-01 to 2019-12, how many times did class
2 occur per month?
Any ideas on how to manipulate the data to be in this way?