我想检查当前行的值是否小于前n行以及后n行,并将结果存储在新列中。例如,我试图得到一个如下的表,其中n=2。
a b0 15 False1 16 False2 13 True3 17 False4 20 False
a b
0 15 False
1 16 False
2 13 True
3 17 False
4 20 False
先谢谢你。
y1aodyip1#
有一个使用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
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
w8f9ii692#
我们首先使用滚动窗口计算前一个最小值,然后反转 Dataframe ,并在反转的 Dataframe 上使用滚动计算下一个最小值:
import pandas as pddf = pd.DataFrame({'value': np.random.randint(0, 100, size=(10))})n = 2 df['less_than_neighbors'] = Falsedf_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)
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)
2条答案
按热度按时间y1aodyip1#
有一个使用numpy数组的解决方案,col_name是一个包含值的变量,new_col_name是布尔列的名称:
w8f9ii692#
我们首先使用滚动窗口计算前一个最小值,然后反转 Dataframe ,并在反转的 Dataframe 上使用滚动计算下一个最小值: