猿问

使用 Pandas 数据框进行高级重采样

我有 1 分钟的间隔数据。对于同一系列,我想重新采样为两个频率,一个是 30 分钟(粗分辨率),另一个是 5 分钟(精细分辨率)。有粗略和精细分辨率数据的条件,如果数据超过阈值(在本例中为-22),则应在 5 分钟重新采样,否则应在 30 分钟重新采样。我在下面有一个示例数据集:


在这种情况下,我的阈值为 -22,如果值小于 -22,则以精细分辨率(5 分钟)重新采样,否则以粗分辨率(30 分钟)重新采样


2018-03-20 08:02:00   -21.344299

2018-03-20 08:03:00   -21.303697

2018-03-20 08:04:00   -21.245916

2018-03-20 08:05:00   -21.328162

2018-03-20 08:06:00   -21.296409

2018-03-20 08:07:00   -21.318793

2018-03-20 08:08:00   -21.259450

2018-03-20 08:09:00   -21.346382

2018-03-20 08:10:00   -21.424463

2018-03-20 08:11:00   -21.466628

2018-03-20 08:12:00   -21.408326

2018-03-20 08:13:00   -21.346902

2018-03-20 08:14:00   -21.374491

2018-03-20 08:15:00   -21.536902

2018-03-20 08:16:00   -21.638408

2018-03-20 08:17:00   -21.547834

2018-03-20 08:18:00   -21.606655

2018-03-20 08:19:00   -21.674846

2018-03-20 08:20:00   -21.728983

2018-03-20 08:21:00   -22.026737

2018-03-20 08:22:00   -21.530134

2018-03-20 08:23:00   -21.430710

2018-03-20 08:24:00   -21.530134

2018-03-20 08:25:00   -21.471833

2018-03-20 08:26:00   -21.473395

2018-03-20 08:27:00   -21.505669

2018-03-20 08:28:00   -21.530655

2018-03-20 08:29:00   -21.654545

2018-03-20 08:30:00   -21.902847

2018-03-20 08:31:00   -21.411970

                         ...    

2018-03-24 13:33:00   -22.319808

2018-03-24 13:34:00   -22.272957

2018-03-24 13:35:00   -22.338546

2018-03-24 13:36:00   -22.242244

2018-03-24 13:37:00   -22.299506

2018-03-24 13:38:00   -22.181342

2018-03-24 13:39:00   -22.219341

2018-03-24 13:40:00   -22.281286

2018-03-24 13:41:00   -22.399453

2018-03-24 13:42:00   -22.049120

2018-03-24 13:43:00   -22.283889


在这种情况下,我期望这样的答案:


2018-03-20 08:02:00   -21.4889   (This is mean over 30 min)

2018-03-24 13:33:00   -22.2946   (This is mean over 5 min)

2018-03-24 13:38:00   -22.2261

2018-03-24 13:43:00   -22.177

这个问题有什么内置功能吗?


明月笑刀无情
浏览 170回答 1
1回答

米琪卡哇伊

让我们试试这个:df = df.set_index(0)g = df[1].lt(-22).mul(1).diff().bfill().ne(0).cumsum()df.groupby(g).apply(lambda x: x.resample('5T', kind='period').mean().reset_index()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (x.iloc[0] < -22).any() else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.resample('30T', kind='period').mean().reset_index())\&nbsp; &nbsp;.reset_index(drop=True)输出:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10&nbsp; 2018-03-20 08:02 -21.4314501&nbsp; 2018-03-20 08:21 -22.0267372&nbsp; 2018-03-20 08:22 -21.5441893&nbsp; 2018-03-24 13:33 -22.2946124&nbsp; 2018-03-24 13:38 -22.2261085&nbsp; 2018-03-24 13:43 -22.2366496&nbsp; 2018-03-24 13:47 -21.9382447&nbsp; 2018-03-24 13:48 -22.1809938&nbsp; 2018-03-24 13:51 -21.9689569&nbsp; 2018-03-24 13:56 -22.24474310 2018-03-24 14:01 -22.006955
随时随地看视频慕课网APP

相关分类

Python
我要回答