[英]Dataframe from Series grouped by weekday and hour of day
I have a Series with a DatetimeIndex, as such:我有一个带有 DatetimeIndex 的系列,例如:
time my_values
2017-12-20 09:00:00 0.005611
2017-12-20 10:00:00 -0.004704
2017-12-20 11:00:00 0.002980
2017-12-20 12:00:00 0.001497
...
2021-08-20 13:00:00 -0.001084
2021-08-20 14:00:00 -0.001608
2021-08-20 15:00:00 -0.002182
2021-08-20 16:00:00 -0.012891
2021-08-20 17:00:00 0.002711
I would like to create a dataframe of average values with the weekdays as columns names and hour of the day as index, resulting in this:我想创建一个平均值的 dataframe ,其中工作日作为列名,一天中的小时作为索引,结果如下:
hour Monday Tuesday ... Sunday
0 0.005611 -0.001083 -0.003467
1 -0.004704 0.003362 -0.002357
2 0.002980 0.019443 0.009814
3 0.001497 -0.002967 -0.003466
...
19 -0.001084 0.009822 0.003362
20 -0.001608 -0.002967 -0.003567
21 -0.002182 0.035600 -0.003865
22 -0.012891 0.002945 -0.002345
23 0.002711 -0.002458 0.006467
How can do this in Python?如何在 Python 中做到这一点?
# Coerce time to datetime
df['time'] = pd.to_datetime(df['time'])
# Extract day and hour
df = df.assign(day=df['time'].dt.strftime('%A'), hour=df['time'].dt.hour)
# Pivot
df.pivot(index='hour', columns='day', values='my_values').reset_index()
Since you asked for a solution that returns the average values, I propose this groupby
solution由于您要求返回平均值的解决方案,因此我建议使用此groupby
解决方案
df["weekday"] = DF.time.dt.strftime('%A')
df["hour"] = DF.time.dt.strftime('%H')
df = df.drop(["time"], axis=1)
# calculate averages by weekday and hour
df2 = DF.groupby(["weekday", "hour"]).mean()
# put it in the right format
df2.reindex(["hour", "weekday"]).unstack()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.