我有一个包含3列的数据框 Date,Time,Value
**Date** **Time** **Value**
7/1/1990 0:00:00 10
7/1/1990 1:00:00 10
7/1/1990 1:15:00 12
7/1/1990 1:50:00 11
7/1/1990 2:00:00 10
7/1/1990 3:00:00 10
7/1/1990 3:30:00 12
7/1/1990 4:00:00 11
....
8/1/1990 0:00:00 11
8/1/1990 1:00:00 12
8/1/1990 2:00:00 11
8/1/1990 2:50:00 11
8/1/1990 3:00:00 12
8/1/1990 4:00:00 10
8/1/1990 4:30:00 12
.....
我想通过对0:00:00到0:59:59或4:00:00到4:59:59这样的时间段取平均值来合并所有行
**Date** **Time** **Value**
7/1/1990 0:00:00 10
7/1/1990 1:00:00 11
7/1/1990 2:00:00 10
7/1/1990 3:00:00 11
7/1/1990 4:00:00 11
....
8/1/1990 0:00:00 11
8/1/1990 1:00:00 12
8/1/1990 2:00:00 11
8/1/1990 3:00:00 12
8/1/1990 4:00:00 11
.....
答案 0 :(得分:1)
使用pandas.to_datetime
和pandas.DataFrame.resample
:
import pandas as pd
df.index = pd.to_datetime(df[['Date', 'Time']].apply(' '.join, 1), dayfirst=True)
new_df = df.resample('1H').mean().dropna().reset_index()
print(new_df)
输出:
index Value
0 1990-01-07 00:00:00 10.0
1 1990-01-07 01:00:00 11.0
2 1990-01-07 02:00:00 10.0
3 1990-01-07 03:00:00 11.0
4 1990-01-07 04:00:00 11.0
5 1990-01-08 00:00:00 11.0
6 1990-01-08 01:00:00 12.0
7 1990-01-08 02:00:00 11.0
8 1990-01-08 03:00:00 12.0
9 1990-01-08 04:00:00 11.0