【发布时间】:2021-09-29 03:44:41
【问题描述】:
我有一个降水值的时间序列数据框
print(rain_df)
date precip
0 2017-01-10 0.0
1 2017-01-17 1.0
2 2017-01-24 1.0
3 2017-01-31 4.0
4 2017-02-07 1.0
.. ... ...
218 2021-04-27 1.7
219 2021-05-03 22.7
220 2021-05-10 0.0
221 2021-05-17 2.0
222 2021-05-25 0.2
rain_df = rain_df.join(model_data['date'].dt.month.astype(str).str.get_dummies())
rain_df = rain_df.join(rain_df['date'].dt.year.astype(str).str.get_dummies())
rain_df = rain_df[rain_df['precip']>0]
rain_df.reset_index(inplace=True,drop=True)
print(rain_df)
date precip 1 10 11 12 2 3 4 5 6 7 8 9 2017 2018 \
0 2017-01-17 1.0 1 0 0 0 0 0 0 0 0 0 0 0 1 0
1 2017-01-24 1.0 1 0 0 0 0 0 0 0 0 0 0 0 1 0
2 2017-01-31 4.0 1 0 0 0 0 0 0 0 0 0 0 0 1 0
3 2017-02-07 1.0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
4 2017-02-14 22.9 0 0 0 0 1 0 0 0 0 0 0 0 1 0
.. ... ... .. .. .. .. .. .. .. .. .. .. .. .. ... ...
175 2021-03-31 18.3 0 0 0 0 0 1 0 0 0 0 0 0 0 0
176 2021-04-27 1.7 0 0 0 0 0 0 1 0 0 0 0 0 0 0
177 2021-05-03 22.7 0 0 0 0 0 0 0 1 0 0 0 0 0 0
178 2021-05-17 2.0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
179 2021-05-25 0.2 0 0 0 0 0 0 0 1 0 0 0 0 0 0
2019 2020 2021
0 0 0 0
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
.. ... ... ...
175 0 0 1
176 0 0 1
177 0 0 1
178 0 0 1
179 0 0 1
如何创建一个箱线图,其中 x 轴是月-年,y 轴是 precip 值?
这是我的尝试
# reverse one-hot encoding
rain_df['month-year'] = (rain_df.iloc[:, 2:] == 1).idxmax(1)
rain_df = rain_df.melt(id_vars='month-year',value_vars='precip', value_name='precip')
print(rain_df)
month-year variable precip
0 1 precip 1.0
1 1 precip 1.0
2 1 precip 4.0
3 2 precip 1.0
4 2 precip 22.9
.. ... ... ...
175 3 precip 18.3
176 4 precip 1.7
177 5 precip 22.7
178 5 precip 2.0
179 5 precip 0.2
ax=sn.boxplot(x='month-year', y='precip', hue='variable', data=rain_df, palette="Set3", linewidth=1)
ax.set_title('Joliette')
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
这里的问题是它只在 x 轴上绘制月份,没有关于给定年份的信息。我是不是搞砸了我的 melt 函数或其他什么?
【问题讨论】:
标签: python pandas datetime seaborn