Swapnil Shankar
Swapnil Shankar

Reputation: 45

Plotting in python using group by and sum

I am trying to plot a graph using the below data. I need to have graph Year vs Txns

Original Data which is dataset1= in the code

WeekDay Day Month   Year    Time    Txns
   1     5    1     2015      3       1
   1     5    1     2015      4       4
   1     5    1     2015      5       1
   1     5    1     2015      7       2

This is the data after group by and sum which is plot= in the code

Index       Txns
(2014, 12)  5786
(2015, 1)   70828
(2015, 2)   63228
(2015, 3)   74320

my code:

plot = dataset1.groupby(['Year', 'Month'])['Txns'].sum()
plot_df = plot.unstack('Month').loc[:, 'Txns']
plot_df.index = pd.PeriodIndex(plot_df.index.tolist(), freq='2015')
plot_df.plot()

I get this error everytime:

KeyError: 'the label [Txns] is not in the [columns]'

How can I fix this?

Upvotes: 3

Views: 11352

Answers (1)

abolotnov
abolotnov

Reputation: 4332

Why not just data.groupby('Year').Txns.sum() if you want to group by year and sum Txns?

and .plot() if you wanted to plot it:

enter image description here

or yearly lines:

enter image description here

Upvotes: 4

Related Questions