英文:
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] < 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
答案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({'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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论