简体   繁体   English

Python中的Groupby和Plot条形图

[英]Groupby and Plot bar graph in Python

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. x轴为'year' ,y轴为每年每周销售额的总和。 While plotting I am getting 'KeyError: 'year' . 在策划时我得到'KeyError: 'year' I guess it's because 'year' became index during group by. 我想这是因为'year'成为了分组中的指数。

Below is the sample content from csv file: 以下是来自csv文件的示例内容:

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: 更新了代码,下面是输出: DataFrame输出:

   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: 下面是我得到的图表: 在此输入图像描述

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 . 在读取csv文件时,需要使用空格作为delim_whitespace=True的分隔符,然后在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 产量

在此输入图像描述

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)

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM