Groupby and Plot bar graph in Python

by user1882624   Last Updated September 17, 2018 03:26 AM

I want to plot a bar graph for sales over period of year. x-axis as 'year' and y-axis as sum of weekly sales per year. While plotting I am getting 'KeyError: 'year'. I guess it's because 'year' became index during group by.

Below is the sample content from csv file:

Store   year    Weekly_Sales
1   2014    24924.5
1   2010    46039.49
1   2015    41595.55
1   2010    19403.54
1   2015    21827.9
1   2010    21043.39
1   2014    22136.64
1   2010    26229.21
1   2014    57258.43
1   2010    42960.91

Below is the code I used to group by

storeDetail_df = pd.read_csv('Details.csv')
result_group_year= storeDetail_df.groupby(['year'])
total_by_year = result_group_year['Weekly_Sales'].agg([np.sum])

total_by_year.plot(kind='bar' ,x='year',y='sum',rot=0)

Updated the Code and below is the output: DataFrame output:

   year          sum
0  2010  42843534.38
1  2011  45349314.40
2  2012  35445927.76
3  2013         0.00

below is the Graph i am getting: enter image description here



Answers 3


In case it is making year your index due to group by command. you need to remove it as a index before plotting. Try

total_by_year = total_by_year.reset_index(drop=False, inplace=True)
SaKu.
SaKu.
September 16, 2018 16:05 PM

You might want to try this

storeDetail_df = pd.read_csv('Details.csv')
result_group_year= storeDetail_df.groupby(['year'])['Weekly_Sales'].sum()

result_group_year = result_group_year.reset_index(drop=False)
result_group_year.plot.bar(x='year', y='Weekly_Sales')
Ravi
Ravi
September 16, 2018 16:05 PM

While reading your csv file, you needed to use white space as the delimiter as delim_whitespace=True and then reset the index after summing up the Weekly_Sales. Below is the working code:

storeDetail_df = pd.read_csv('Details.csv', delim_whitespace=True)
result_group_year= storeDetail_df.groupby(['year'])
total_by_year = result_group_year['Weekly_Sales'].agg([np.sum]).reset_index()
total_by_year.plot(kind='bar' ,x='year',y='sum',rot=0,  legend=False)

Output

enter image description here

Bazingaa
Bazingaa
September 16, 2018 16:06 PM

Related Questions


pandas groupby apply is really slow

Updated November 05, 2017 15:26 PM

Do not map item to any output using apply()

Updated July 30, 2018 21:26 PM


Chaining groupby and apply pandas

Updated May 22, 2018 02:26 AM