简体   繁体   English

访问Pandas.Dataframe中的邻居行

[英]Access neighbour rows in Pandas.Dataframe

I'm trying to calculate local max and min for a series of data: if current row value is greater or lower both following and preceding row, set it to current value, else set to NaN. 我正在尝试计算一系列数据的局部最大值和最小值:如果当前行的值大于或小于后一行和前一行,则将其设置为当前值,否则设置为NaN。 Is there any more elegant way to do it, other than this one: 除此以外,还有其他更优雅的方法:

import pandas as pd
import numpy as np

rng = pd.date_range('1/1/2014', periods=10, freq='5min')
s = pd.Series([1, 2, 3, 2, 1, 2, 3, 5, 7, 4], index=rng)
df = pd.DataFrame(s, columns=['val'])
df.index.name = "dt"
df['minmax'] = np.NaN

for i in range(len(df.index)):
    if i == 0:
        continue
    if i == len(df.index) - 1:
        continue
    if df['val'][i] >= df['val'][i - 1] and df['val'][i] >= df['val'][i + 1]:
        df['minmax'][i] = df['val'][i]
        continue
    if df['val'][i] <= df['val'][i - 1] and df['val'][i] <= df['val'][i + 1]:
        df['minmax'][i] = df['val'][i]
        continue

print(df)

Result is: 结果是:

                     val  minmax
dt                              
2014-01-01 00:00:00    1     NaN
2014-01-01 00:05:00    2     NaN
2014-01-01 00:10:00    3       3
2014-01-01 00:15:00    2     NaN
2014-01-01 00:20:00    1       1
2014-01-01 00:25:00    2     NaN
2014-01-01 00:30:00    3     NaN
2014-01-01 00:35:00    5     NaN
2014-01-01 00:40:00    7       7
2014-01-01 00:45:00    4     NaN

We can use shift and where to determine what to assign the values, importantly we have to use the bit comparators & and | 我们可以使用shift以及where确定分配值的方式,重要的是,我们必须使用位比较器&| when comparing series. 比较系列时。 Shift will return a Series or DataFrame shifted by 1 row (default) or the passed value. Shift将返回一个Series或DataFrame偏移1行(默认值)或传递的值。

When using where we can pass a boolean condition and the second param NaN tells it to assign this value if False . 当使用where我们可以通过一个布尔条件和第二PARAM NaN ,如果告诉它分配该值False

In [81]:

df['minmax'] = df['val'].where(((df['val'] < df['val'].shift(1))&(df['val'] < df['val'].shift(-1)) | (df['val'] > df['val'].shift(1))&(df['val'] > df['val'].shift(-1))), NaN)
df
Out[81]:
                     val  minmax
dt                              
2014-01-01 00:00:00    1     NaN
2014-01-01 00:05:00    2     NaN
2014-01-01 00:10:00    3       3
2014-01-01 00:15:00    2     NaN
2014-01-01 00:20:00    1       1
2014-01-01 00:25:00    2     NaN
2014-01-01 00:30:00    3     NaN
2014-01-01 00:35:00    5     NaN
2014-01-01 00:40:00    7       7
2014-01-01 00:45:00    4     NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM