熊猫的重新采样-获取平均值。时间段

时间:2018-07-28 09:31:07

标签: python pandas datetime resampling

我有一个df,时间戳记为INDEX(dtype='datetime64[ns]),并且1Y的关联值为:

                     values
Timestamp   
2014-01-01 00:00:00 20.155100
2014-01-01 00:15:00 7.586481
2014-01-01 00:30:00 73.115602
2014-01-01 00:45:00 5.936765
2014-01-01 01:00:00 82.130244

...

现在我想确定每天,每周,每月和每季度的08:00:00到20:00:00之间所有值的平均值吗?

是否有适当的pd.resample选项?如果没有,有什么建议吗?

1 个答案:

答案 0 :(得分:0)

有一些解决方法:

首先按如下所示导入df

timestamp   values
0   2014-01-01 00:00:00 20.155100
1   2014-01-01 00:15:00 7.586481
2   2014-01-01 00:30:00 73.115602
3   2014-01-01 00:45:00 5.936765
4   2014-01-01 01:00:00 82.130244

然后将一个time.hour列应用于df并应用您的条件逻辑:

df["hour"] = df["timestamp"].apply(lambda time: time.hour)

Output:

timestamp   values  hour
0   2014-01-01 00:00:00 20.155100   0
1   2014-01-01 00:15:00 7.586481    0
2   2014-01-01 00:30:00 73.115602   0
3   2014-01-01 00:45:00 5.936765    0
4   2014-01-01 01:00:00 82.130244   1

df_new = df[(df["hour"]>=8) & (df["hour"] < 20)].drop("hour", axis=1) #apply conditional logic to a Pandas DataFrame
df_new.set_index("timestamp").resample("1D").mean().head() #resample

Output:

               value
timestamp   
2014-01-01  46.139711
2014-01-02  48.794015
2014-01-03  48.818879
2014-01-04  50.030388
2014-01-05  55.124282