【问题标题】:Resampling and add threshold information in Pandas dataframe在 Pandas 数据框中重新采样并添加阈值信息
【发布时间】:2018-09-18 21:04:04
【问题描述】:

我的 pandas 数据帧频率为 1 分钟,我想根据阈值数据进行重新采样(numpy 数组中有多个阈值)

这是我的数据集示例:

2018-01-01 00:01:00    0.867609
2018-01-01 00:02:00    0.544493
2018-01-01 00:03:00    0.958497
2018-01-01 00:04:00    0.371790
2018-01-01 00:05:00    0.470320
2018-01-01 00:06:00    0.757448
2018-01-01 00:07:00    0.198261
2018-01-01 00:08:00    0.666350
2018-01-01 00:09:00    0.392574
2018-01-01 00:10:00    0.627608
2018-01-01 00:11:00    0.414380
2018-01-01 00:12:00    0.120925
2018-01-01 00:13:00    0.559495
2018-01-01 00:14:00    0.260619
2018-01-01 00:15:00    0.982731
2018-01-01 00:16:00    0.996133
2018-01-01 00:17:00    0.410816
2018-01-01 00:18:00    0.366457
2018-01-01 00:19:00    0.927745
2018-01-01 00:20:00    0.626804
2018-01-01 00:21:00    0.223193
2018-01-01 00:22:00    0.007136
2018-01-01 00:23:00    0.245006
2018-01-01 00:24:00    0.491245
2018-01-01 00:25:00    0.215716
2018-01-01 00:26:00    0.932378
2018-01-01 00:27:00    0.366263
2018-01-01 00:28:00    0.522177
2018-01-01 00:29:00    0.614966
2018-01-01 00:30:00    0.670983

threshold=np.array([0.5,0.8,0.9])

我想要的是提取超过阈值的数据,如果不超过阈值,只需在 30 分钟重新采样数据

示例答案:

                                   Threshold
2018-01-01 00:01:00    0.867609        0.8
2018-01-01 00:02:00    0.544493        0.5
2018-01-01 00:03:00    0.958497        0.9
2018-01-01 00:05:00    0.421055        NA
2018-01-01 00:06:00    0.757448        0.5
2018-01-01 00:07:00    0.198261        NA
2018-01-01 00:08:00    0.666350        0.5
2018-01-01 00:09:00    0.392574        NA
2018-01-01 00:10:00    0.627608        0.5
2018-01-01 00:12:00    0.414380        NA
2018-01-01 00:13:00    0.559495        0.5
2018-01-01 00:14:00    0.260619        NA
2018-01-01 00:15:00    0.982731        0.9
2018-01-01 00:16:00    0.996133        0.9
2018-01-01 00:18:00    0.388636        NA
2018-01-01 00:19:00    0.927745        0.9
2018-01-01 00:20:00    0.626804        0.5
2018-01-01 00:25:00    0.215716        NA
2018-01-01 00:26:00    0.932378        0.9
2018-01-01 00:27:00    0.366263        NA
2018-01-01 00:28:00    0.522177        0.5
2018-01-01 00:29:00    0.614966        0.5
2018-01-01 00:30:00    0.670983        0.5

我从@Scott Boston 那里得到了重采样的解决方案,

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('1T', kind='period').mean().reset_index()
                           if (x.iloc[0] < -22).any() else 
                              x.resample('30T', kind='period').mean().reset_index())\
   .reset_index(drop=True)

【问题讨论】:

  • @ScottBoston 我已经改写了我的问题,希望现在清楚。
  • 是的,这样好多了。感谢您的简化和澄清。

标签: python pandas numpy


【解决方案1】:

使用pd.cut:

threshold=np.array([0.5,0.8,0.9]).tolist()
pd.cut(df[1],bins=threshold+[np.inf],labels=threshold)

输出:

0     0.8
1     0.5
2     0.9
3     NaN
4     NaN
5     0.5
6     NaN
7     0.5
8     NaN
9     0.5
10    NaN
11    NaN
12    0.5
13    NaN
14    0.9
15    0.9
16    NaN
17    NaN
18    0.9
19    0.5
20    NaN
21    NaN
22    NaN
23    NaN
24    NaN
25    0.9
26    NaN
27    0.5
28    0.5
29    0.5
Name: 1, dtype: category
Categories (3, float64): [0.5 < 0.8 < 0.9]

现在,让我们将其添加到 datafame 并过滤掉所有连续的 NaN。

df['Threshold'] = pd.cut(df[1],bins=threshold+[np.inf],labels=threshold)
mask = ~(df.Threshold.isnull() & (df.Threshold.isnull() == df.Threshold.isnull().shift(1)))
df[mask]

输出:

                      0         1 Threshold
0   2018-01-01 00:01:00  0.867609       0.8
1   2018-01-01 00:02:00  0.544493       0.5
2   2018-01-01 00:03:00  0.958497       0.9
3   2018-01-01 00:04:00  0.371790       NaN
5   2018-01-01 00:06:00  0.757448       0.5
6   2018-01-01 00:07:00  0.198261       NaN
7   2018-01-01 00:08:00  0.666350       0.5
8   2018-01-01 00:09:00  0.392574       NaN
9   2018-01-01 00:10:00  0.627608       0.5
10  2018-01-01 00:11:00  0.414380       NaN
12  2018-01-01 00:13:00  0.559495       0.5
13  2018-01-01 00:14:00  0.260619       NaN
14  2018-01-01 00:15:00  0.982731       0.9
15  2018-01-01 00:16:00  0.996133       0.9
16  2018-01-01 00:17:00  0.410816       NaN
18  2018-01-01 00:19:00  0.927745       0.9
19  2018-01-01 00:20:00  0.626804       0.5
20  2018-01-01 00:21:00  0.223193       NaN
25  2018-01-01 00:26:00  0.932378       0.9
26  2018-01-01 00:27:00  0.366263       NaN
27  2018-01-01 00:28:00  0.522177       0.5
28  2018-01-01 00:29:00  0.614966       0.5
29  2018-01-01 00:30:00  0.670983       0.5

【讨论】:

    猜你喜欢
    • 2018-11-26
    • 2019-04-15
    • 2014-10-04
    • 2017-09-19
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    相关资源
    最近更新 更多