Prolle
Prolle

Reputation: 358

How to Bar Chart (with Dates on x-axis) after Groupby operation

I have a large df where I have used a group operation on "topic_nmf" and "dates" and counted occurrences of topic_nmf

sample DF

df3 = pd.DataFrame({'topic_nmf':[0,0,0,0,0,1,1,1,2,2], 'date':['2020-08','2020-06','2020-05','2020-02','2019-11','2019-08','2020-03','2020-02', '2019-10','2019-09'], 'count':[3,5,3,5,6,3,3,9,2,1]})

Output

The real df is much larger (it has 40 top_nmf)

I wish to do either a line plot, or a bar chart with the 'date'(year/month) on the x-axes and the 'count' on the y-axis.

Each 'topic_nmf' is in itself a trendline, or color coded bar on the bar chart.

My attempt is as follows:

plt.bar(df3.date, df3.count)

but I get the following error:

TypeError: unsupported operand type(s) for +: 'int' and 'method'

Any help appreciated.

Update: Thanks that does help, although i am not quite there

df3.set_index('date').plot.bar()

df3.set_index('date').plot()

as suggested, gives

However,I am seeking three trendlines (each labelled 0,1,2) and same for bar chart?

Upvotes: 0

Views: 931

Answers (1)

wwnde
wwnde

Reputation: 26676

Set date as index. By default, index is plotted on the x axis.

df3.set_index('date').plot.bar()

df3.set_index('date').plot()

Following your comments. Please try

df3.groupby(['date','topic_nmf'])['count'].sum().unstack().plot.bar()
#df3.groupby(['date','topic_nmf'])['count'].sum().unstack().plot()#lineplot

enter image description here

Upvotes: 1

Related Questions