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

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

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

问题

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

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

  1. a b
  2. 0 15 False
  3. 1 16 False
  4. 2 13 True
  5. 3 17 False
  6. 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.

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

Thank you in advance.

答案1

得分: 0

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

  1. def checker(df, col_name, new_col_name, n):
  2. vals = df[col_name].to_numpy()
  3. bools = [(vals[i] < vals[max(i - n, 0) : i]).all()
  4. and (vals[i] < vals[i + 1 : min(i + n + 1, vals.size)]).all()
  5. for i in range(vals.size)]
  6. df[new_col_name] = bools
  7. 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:

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

答案2

得分: 0

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

  1. import pandas as pd
  2. df = pd.DataFrame({'value': np.random.randint(0, 100, size=(10))})
  3. n = 2
  4. df['less_than_neighbors'] = False
  5. df_reversed = df[::-1]
  6. df['min_prev'] = df['value'].rolling(window=n, min_periods=n, closed='left').min()
  7. df['min_next'] = df_reversed['value'].rolling(window=n, min_periods=n, closed='left').min()
  8. df.loc[((df['value'] < df['min_prev']) & (df['value'] < df['min_next'])), 'less_than_neighbors'] = True
  9. #df.drop(columns=['min_prev', 'min_next'], inplace=True)
  10. 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:

  1. import pandas as pd
  2. df = pd.DataFrame({&#39;value&#39;: np.random.randint(0, 100, size=(10))})
  3. n = 2
  4. df[&#39;less_than_neighbors&#39;] = False
  5. df_reversed = df[::-1]
  6. df[&#39;min_prev&#39;] = df[&#39;value&#39;].rolling(window=n, min_periods=n, closed=&#39;left&#39;).min()
  7. df[&#39;min_next&#39;] = df_reversed[&#39;value&#39;].rolling(window=n, min_periods=n, closed=&#39;left&#39;).min()
  8. 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
  9. #df.drop(columns=[&#39;min_prev&#39;, &#39;min_next&#39;], inplace=True)
  10. 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:

确定