我试图从数据帧中获取最后8小时的数据。有没有办法实现这个目标?
假设我有一个带时间戳的数据框:
Time X Y
19/8/2017 10:00:00 824.93 6.42
19/8/2017 10:20:00 825.88 6.41
19/8/2017 10:40:00 825.59 6.45
19/8/2017 11:00:00 826.18 6.6
19/8/2017 11:20:00 825.2 6.62
19/8/2017 11:40:00 825.3 6.65
19/8/2017 12:00:00 824.72 6.54
19/8/2017 12:20:00 825.72 6.45
19/8/2017 12:40:00 824.99 6.35
19/8/2017 13:00:00 824.63 6.31
19/8/2017 13:20:00 825.87 6.34
19/8/2017 13:40:00 826.12 6.29
19/8/2017 14:00:00 825.41 6.31
19/8/2017 14:20:00 825.34 6.36
19/8/2017 14:40:00 825.15 6.39
19/8/2017 15:00:00 824.87 6.15
19/8/2017 15:20:00 825.57 6.17
19/8/2017 15:40:00 825.35 6.2
19/8/2017 16:00:00 824.94 6.26
19/8/2017 16:20:00 825.86 6.39
19/8/2017 16:40:00 824.74 6.39
19/8/2017 17:00:00 823.46 6.33
19/8/2017 17:20:00 824.46 6.41
19/8/2017 17:40:00 824.67 6.4
19/8/2017 18:00:00 824.38 6.36
19/8/2017 18:20:00 825.23 6.37
19/8/2017 18:40:00 826.09 6.37
19/8/2017 19:00:00 824.77 6.32
19/8/2017 19:20:00 824.18 6.34
19/8/2017 19:40:00 823.82 6.33
19/8/2017 20:00:00 825 6.39
19/8/2017 20:20:00 825.01 6.4
19/8/2017 20:40:00 824.24 6.45
19/8/2017 21:00:00 817.65 6.17
19/8/2017 21:20:00 781.58 4.92
19/8/2017 21:40:00 739.89 3.51
我想从此数据框中提取最后8小时的数据。
我试过了,但我无能为力:
last8 = (df["Time"] == (dfs["Time"].shift() + Timedelta(hours = 8))).cumsum()
任何帮助都将不胜感激。
答案 0 :(得分:2)
从最长时间获得最后8小时(在本案例中为最后一次)
In [2178]: df[df['Time'] > df['Time'].max() - pd.Timedelta(hours=8)]
Out[2178]:
Time X Y
12 2017-08-19 14:00:00 825.41 6.31
13 2017-08-19 14:20:00 825.34 6.36
14 2017-08-19 14:40:00 825.15 6.39
15 2017-08-19 15:00:00 824.87 6.15
16 2017-08-19 15:20:00 825.57 6.17
17 2017-08-19 15:40:00 825.35 6.20
18 2017-08-19 16:00:00 824.94 6.26
19 2017-08-19 16:20:00 825.86 6.39
20 2017-08-19 16:40:00 824.74 6.39
21 2017-08-19 17:00:00 823.46 6.33
22 2017-08-19 17:20:00 824.46 6.41
23 2017-08-19 17:40:00 824.67 6.40
24 2017-08-19 18:00:00 824.38 6.36
25 2017-08-19 18:20:00 825.23 6.37
26 2017-08-19 18:40:00 826.09 6.37
27 2017-08-19 19:00:00 824.77 6.32
28 2017-08-19 19:20:00 824.18 6.34
29 2017-08-19 19:40:00 823.82 6.33
30 2017-08-19 20:00:00 825.00 6.39
31 2017-08-19 20:20:00 825.01 6.40
32 2017-08-19 20:40:00 824.24 6.45
33 2017-08-19 21:00:00 817.65 6.17
34 2017-08-19 21:20:00 781.58 4.92
35 2017-08-19 21:40:00 739.89 3.51
答案 1 :(得分:1)
试试这个:
df[df.Time >= pd.datetime.now() - pd.Timedelta('8H')]