您可以通過hours
和minutes
和transform
mean
嘗試groupby
:
print df
Time some value
0 2010-08-31 12:30:00 33.910
1 2010-08-31 12:40:00 33.250
2 2010-08-31 12:50:00 30.500
3 2010-08-31 13:00:00 27.065
4 2010-08-31 13:10:00 25.610
5 2013-06-07 02:10:00 16.970
6 2013-06-07 02:20:00 16.955
7 2013-06-07 02:30:00 17.000
8 2013-06-07 02:40:00 17.015
9 2013-06-07 02:50:00 16.910
#convert column time to datetime
df['Time'] = pd.to_datetime(df['Time'])
#set index from column Time
df = df.set_index('Time')
print df
some value
Time
2010-08-31 12:30:00 33.910
2010-08-31 12:40:00 33.250
2010-08-31 12:50:00 30.500
2010-08-31 13:00:00 27.065
2010-08-31 13:10:00 25.610
2013-06-07 02:10:00 16.970
2013-06-07 02:20:00 16.955
2013-06-07 02:30:00 17.000
2013-06-07 02:40:00 17.015
2013-06-07 02:50:00 16.910
print df.groupby([df.index.hour, df.index.minute])['some value'].transform('mean')
Time
2010-08-31 12:30:00 33.910
2010-08-31 12:40:00 33.250
2010-08-31 12:50:00 30.500
2010-08-31 13:00:00 27.065
2010-08-31 13:10:00 25.610
2013-06-07 02:10:00 16.970
2013-06-07 02:20:00 16.955
2013-06-07 02:30:00 17.000
2013-06-07 02:40:00 17.015
2013-06-07 02:50:00 16.910
dtype: float64
下一個解決方案未設置index
到Datetimeindex
,使用dt.hour
和dt.minute
,創造新的列newCol
:
print df
Time some value
0 2010-08-31 12:30:00 33.910
1 2010-08-31 12:40:00 33.250
2 2010-08-31 12:50:00 30.500
3 2010-08-31 13:00:00 27.065
4 2010-08-31 13:10:00 25.610
5 2013-06-07 02:10:00 16.970
6 2013-06-07 02:20:00 16.955
7 2013-06-07 02:30:00 17.000
8 2013-06-07 02:40:00 17.015
9 2013-06-07 02:50:00 16.910
#convert column time to datetime
df['Time'] = pd.to_datetime(df['Time'])
print df
Time some value
0 2010-08-31 12:30:00 33.910
1 2010-08-31 12:40:00 33.250
2 2010-08-31 12:50:00 30.500
3 2010-08-31 13:00:00 27.065
4 2010-08-31 13:10:00 25.610
5 2013-06-07 02:10:00 16.970
6 2013-06-07 02:20:00 16.955
7 2013-06-07 02:30:00 17.000
8 2013-06-07 02:40:00 17.015
9 2013-06-07 02:50:00 16.910
df['newCol'] = df.groupby([df['Time'].dt.hour, df['Time'].dt.minute])['some value']
.transform('mean')
print df
Time some value newCol
0 2010-08-31 12:30:00 33.910 33.910
1 2010-08-31 12:40:00 33.250 33.250
2 2010-08-31 12:50:00 30.500 30.500
3 2010-08-31 13:00:00 27.065 27.065
4 2010-08-31 13:10:00 25.610 25.610
5 2013-06-07 02:10:00 16.970 16.970
6 2013-06-07 02:20:00 16.955 16.955
7 2013-06-07 02:30:00 17.000 17.000
8 2013-06-07 02:40:00 17.015 17.015
9 2013-06-07 02:50:00 16.910 16.910
也許,應該'[df.index.hour,df.index.minute]'中GROUPBY。閱讀:*在每個可用時間內的所有可用天數的平均值* – Zero
@jezrael您提出的建議似乎給出了一天所有數據點的單個值,因此不是每個時間步驟平均爲7天的曲線它具有當天每個時間步的每一天的平均值。所以我會有一個7級的陰謀:-) – cattt84
所以可以'[df.index.hour,df.index.minute]'或不?什麼是推薦輸出? – jezrael