如何在pandas中比较当前行与前n行和后n行?

huangapple go评论68阅读模式
英文:

How to compare current row with previous and next n rows in pandas?

问题

我想检查当前行的值是否小于前n行和后n行的值,并将结果存储在新列中。

例如,我正在尝试获取如下表格,其中n=2。

     a     b
0    15    False
1    16    False
2    13    True
3    17    False
4    20    False

提前感谢您。

英文:

I want to check if the value of the current row is less than the previous n rows as well as the next n rows and store the result in a new column.

for example I am trying to get a table such as following where n=2.

     a     b
0    15    False
1    16    False
2    13    True
3    17    False
4    20    False

Thank you in advance.

答案1

得分: 0

这是一个使用NumPy数组的解决方案,col_name是包含您的值的变量,new_col_name是您的布尔列的名称:

def checker(df, col_name, new_col_name, n):
    vals = df[col_name].to_numpy()
    bools = [(vals[i] < vals[max(i - n, 0) : i]).all() 
        and (vals[i] < vals[i + 1 : min(i + n + 1, vals.size)]).all() 
        for i in range(vals.size)]
    df[new_col_name] = bools
    return df
英文:

There is a solution using numpy arrays, col_name is a variable with your values, new_col_name is a name of your Boolean column:

   def checker(df, col_name, new_col_name, n):
       vals = df[col_name].to_numpy()
       bools = [(vals[i] &lt; vals[max(i - n, 0) : i]).all() 
           and (vals[i] &lt; vals[i + 1 : min(i + n + 1, vals.size)]).all() 
           for i in range(vals.size)]
       df[new_col_name] = bools
       return df

答案2

得分: 0

我们首先使用滚动窗口计算先前的最小值,然后将数据框反转并在反转后的数据框上计算下一个最小值:

    import pandas as pd
    df = pd.DataFrame({'value': np.random.randint(0, 100, size=(10))})
    
    n = 2 
    df['less_than_neighbors'] = False
    df_reversed = df[::-1]
    df['min_prev'] = df['value'].rolling(window=n, min_periods=n, closed='left').min()
    df['min_next'] = df_reversed['value'].rolling(window=n, min_periods=n, closed='left').min()
    
    df.loc[((df['value'] < df['min_prev']) & (df['value'] < df['min_next'])), 'less_than_neighbors'] = True
    
    #df.drop(columns=['min_prev', 'min_next'], inplace=True)
    print(df)
英文:

We first calculate the previous minimum using the rolling window, then we reverse the dataframe and calculate the next minimum using rolling on the reversed dataframe:

import pandas as pd
df = pd.DataFrame({&#39;value&#39;: np.random.randint(0, 100, size=(10))})

n = 2 
df[&#39;less_than_neighbors&#39;] = False
df_reversed = df[::-1]
df[&#39;min_prev&#39;] = df[&#39;value&#39;].rolling(window=n, min_periods=n, closed=&#39;left&#39;).min()
df[&#39;min_next&#39;] = df_reversed[&#39;value&#39;].rolling(window=n, min_periods=n, closed=&#39;left&#39;).min()

df.loc[ ( (df[&#39;value&#39;]  &lt; df[&#39;min_prev&#39;]) &amp; (df[&#39;value&#39;]  &lt; df[&#39;min_next&#39;])), &#39;less_than_neighbors&#39;] = True

#df.drop(columns=[&#39;min_prev&#39;, &#39;min_next&#39;], inplace=True)
print(df)

huangapple
  • 本文由 发表于 2023年5月15日 06:01:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定