【发布时间】:2021-10-22 05:58:49
【问题描述】:
我有一年的交通数据存储在一个数据框中。
| study time | volume | month | hour | day | year | weekday | week_of_year | weekend |
|---|---|---|---|---|---|---|---|---|
| 2019-01-01 00:00:00 | 25 | January | 0 | Tuesday | 2019 | 1 | 1 | 0 |
| 2019-01-01 00:00:15 | 25 | January | 0 | Tuesday | 2019 | 1 | 1 | 0 |
| 2019-01-01 00:00:30 | 21 | January | 0 | Tuesday | 2019 | 1 | 1 | 0 |
| 2019-01-02 00:00:00 | 100 | January | 0 | Wednesday | 2019 | 2 | 1 | 0 |
| 2019-01-02 00:00:15 | 2 | January | 0 | Wednesday | 2019 | 2 | 1 | 0 |
| 2019-01-02 00:00:30 | 50 | January | 0 | Wednesday | 2019 | 2 | 1 | 0 |
我想查看体积数据的每小时、每天、每周和每月模式。我是用这个脚本来做的:
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(16,10))
plt.axes(ax[0,0])
countData19_gdf.groupby(['hour','address']).mean().groupby(['hour'])['volume'].mean().plot(x='hour',y='volume')
plt.ylabel("Total averge counts of the stations")
plt.axes(ax[0,1])
countData19_gdf.groupby(['day','address']).mean().groupby(['day'])['volume'].mean().plot(x='day',y='volume')
plt.axes(ax[1,0])
countData19_gdf.groupby(['week_of_year','address']).mean().groupby(['week_of_year'])['volume'].mean().plot(x='week_of_year',y='volume', rot=90)
plt.ylabel("Total averge counts of the stations")
plt.axes(ax[1,1])
countData19_gdf.groupby(['month','address']).mean().groupby(['month'])['volume'].mean().plot(x='month',y='volume', rot=90)
plt.ylabel("Total averge counts of the stations")
ax[0,0].title.set_text('Hourly')
ax[0,1].title.set_text('Daily')
ax[1,0].title.set_text('Weekly')
ax[1,1].title.set_text('Monthly')
plt.savefig('temporal_global.png')
结果看起来像这样,其中工作日是或月份没有排序。
能否请您帮我解决如何对它们进行排序?我尝试将天数排序为整数,但它不起作用。
【问题讨论】:
-
让数据不仅仅是一张图片会很有帮助。
-
希望对你有帮助
-
我确实尝试过,但它会返回一个 AttributeError
AttributeError: 'DataFrame' object has no attribute 'sort_value'例如months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] plt.axes(ax[1,1]) df1['month']= pd.Categorical(df1['month'], categories=months, ordered=True) df1.sort_value(by="month") df1.groupby(['month','address']).mean().groupby(['month'])['volume'].mean().plot(x='month',y='volume', rot=90) plt.ylabel("Total averge counts of the stations") -
这是一个方法,应该是
sort_values而不是sort_value -
天啊!谢谢。我修复了它,它不再抛出错误,但情节是空的!
标签: python pandas matplotlib time-series