如何使用groupby显示日历月份的标准偏差并绘制它?

问题描述 投票:0回答:1

我正在使用的数据集(df)基于鳄梨价格数据集,可以在下面找到:

https://www.kaggle.com/neuromusic/avocado-prices

我正在尝试按年份,月份和鳄梨类型绘制鳄梨的价格波动性(标准差),如下图所示>>

Price Volatility Chart

[我试图这样编码,但我不断收到错误'值'必须是str或字节的实例,而不是浮点数:

cal_years = [2015, 2016, 2017]
types = ['conventional','organic']
month_dict = {1 : 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
fig, ax = plt.subplots(2,3, figsize=(15,10), sharey=True)
for type in types:
    for axy in range(2):
        for axx in range(3):            
                df_plot = df[(df['type'] == types[axy]) & (df['year'] == cal_years[axx])]
                df_plot = df_plot.groupby('Month', as_index=False)['AveragePrice'].agg(['std'])
                df_plot['Month'] = df_plot['Month'].map(month_dict)                
                ax[axy, axx].plot('Month','AveragePrice',data=df_plot, color='blue',marker='o')
                ax[axy, axx].set_title(str(cal_years[axx]) + ' ' + types[axy])
                ax[axy, axx].tick_params(axis='x', rotation=45)
fig.suptitle('Monthly price volatility', size=16)

如果有人能指出为什么代码无法按我预期的那样工作,我将不胜感激。非常感谢

我正在使用的数据集(df)基于鳄梨价格数据集,可以在下面找到:https://www.kaggle.com/neuromusic/avocado-prices我正在尝试绘制价格波动率(标准.. 。

python statistics data-visualization kaggle
1个回答
0
投票

这里有一个小错误:

© www.soinside.com 2019 - 2024. All rights reserved.