在 python 中的时间序列数据帧上绘制排序的 weekdays/month
plot the sorted weekdays/month on timeseries dataframe in python
我在数据框中存储了一年的交通数据。
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')
结果如下所示,其中工作日或月份未排序。
你能帮我如何对它们进行排序吗?我试图将天数排序为整数,但它不起作用。
groupby 方法将自动对索引进行排序,但是对于字符串值,这意味着按字母顺序排序(而不是按工作日的顺序)。
您可以做的是使用 reindex
方法让索引按您喜欢的方式排序。例如:
countData19_gdf.groupby(['day','address']).mean().groupby(['day'])['volume'].mean().reindex(['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']).plot(x='day',y='volume')
注:
如果索引中的值不在 reindex
方法中指定的值列表中,则不会包括该行。同样,如果该列表中有一个新值,但该值不存在于索引中,则会导致将 NaN
值分配给该新索引。因此,如果您的 countData19_gdf
没有 day
,例如 Monday
,它将出现在重新索引的 df
中,但该值将设置为 NaN
.
编辑:
由于您已经有了工作日的数值(您可能希望几个月都得到相同的数值),为了避免手动指定新索引,您可以通过以下方式获得排序的字符串值:
countData19_gdf.sort_values(by = 'weekday')['day'].unique()
快速示例(我更改了给定数据中的一些 'day' 值以显示问题):
df.groupby(['day','address']).mean().groupby(['day'])['volume'].mean().plot(x='day',y='volume')
输出:
df.groupby(['day','address']).mean().groupby(['day'])['volume'].mean().reindex(['Tuesday','Wednesday','Friday']).plot(x='day',y='volume')
输出:
我在数据框中存储了一年的交通数据。
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')
结果如下所示,其中工作日或月份未排序。
你能帮我如何对它们进行排序吗?我试图将天数排序为整数,但它不起作用。
groupby 方法将自动对索引进行排序,但是对于字符串值,这意味着按字母顺序排序(而不是按工作日的顺序)。
您可以做的是使用 reindex
方法让索引按您喜欢的方式排序。例如:
countData19_gdf.groupby(['day','address']).mean().groupby(['day'])['volume'].mean().reindex(['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']).plot(x='day',y='volume')
注:
如果索引中的值不在 reindex
方法中指定的值列表中,则不会包括该行。同样,如果该列表中有一个新值,但该值不存在于索引中,则会导致将 NaN
值分配给该新索引。因此,如果您的 countData19_gdf
没有 day
,例如 Monday
,它将出现在重新索引的 df
中,但该值将设置为 NaN
.
编辑:
由于您已经有了工作日的数值(您可能希望几个月都得到相同的数值),为了避免手动指定新索引,您可以通过以下方式获得排序的字符串值:
countData19_gdf.sort_values(by = 'weekday')['day'].unique()
快速示例(我更改了给定数据中的一些 'day' 值以显示问题):
df.groupby(['day','address']).mean().groupby(['day'])['volume'].mean().plot(x='day',y='volume')
输出:
df.groupby(['day','address']).mean().groupby(['day'])['volume'].mean().reindex(['Tuesday','Wednesday','Friday']).plot(x='day',y='volume')
输出: