Pandas Dataframe,获取每个星期一的平均值1 Am

时间:2018-06-11 21:17:15

标签: python pandas dataframe types average

我的DataFrame看起来像这样:

index                   value
2016-03-21 00:00:00     0.613014
2016-03-21 01:00:00     0.596383
2016-03-21 02:00:00     0.623570
2016-03-21 03:00:00     0.663350
2016-03-21 04:00:00     0.677817
2016-03-21 05:00:00     0.727116
2016-03-21 06:00:00     0.920279
2016-03-21 07:00:00     1.205863
2016-03-21 08:00:00     0.880946
2016-03-21 09:00:00     0.186947
2016-03-21 10:00:00     -0.563276
2016-03-21 11:00:00     -1.249595
2016-03-21 12:00:00     -1.596035
2016-03-21 13:00:00     -1.886954
2016-03-21 14:00:00     -1.912325
2016-03-21 15:00:00     -1.750623
...     
2016-06-20 23:00:00     2.125791

我想在数据帧中获取每个星期一凌晨1点的平均值。最后,我希望得到一个代表平均周数的输出。数据框,所以我可以想象一周的过程。

我希望我能表达清楚。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我不确定如何解释你的问题,所以这里有两个答案(针对不同的问题)

获取星期一凌晨1点发生的所有值的平均值(输出将是单个标量)

# Make sure the index is a pd.datetime object
df.index = pd.to_datetime(df.index)

# find all rows which occur on a monday and at 01:00:00, and take the mean
monday_means = (df.loc[(df.index.weekday_name == 'Monday') &
                       (df.index.time == pd.to_datetime('01:00:00').time())]
                .mean()
                .to_frame('Monday 1 Am'))

得到前一周的平均值,周一从凌晨1点开始(输出将是一系列)

# Make sure the index is a pd.datetime object
df.index = pd.to_datetime(df.index)

# Create a column for week number, which counts consecutively every monday at 1:00:00
df['week_number'] = ((df.index.weekday_name == 'Monday') &
                     (df.index.time == pd.to_datetime('01:00:00').time())
                     .cumsum())


# Groupby week number and get the mean
df.groupby('week_number').mean()

或者,更简单(但不太灵活,它将在星期一的午夜开始,而不是凌晨1点):

df.groupby(pd.Grouper(freq='W')).mean()