英文:
Dataframe : replace value and values around based on condition
问题
我想创建一个筛选器,根据条件替换数据框列中的值,同时还包括周围的值。
例如,如果某个值大于45,我想将其替换为NaN,即使它周围的值不符合条件:
df[i] = 10, 12, 25, 60, 32, 26, 23
在这个例子中,筛选器应该将60替换为NaN,以及它前面的值(25)和后面的值(32)。筛选的结果将是:
df[i] = 10, 12, NaN, NaN, NaN, 26, 23
到目前为止,我正在使用以下代码,但它只会替换符合条件的值,而不包括周围的值:
df[i].where(df[i] <= 45, np.nan, inplace=True)
英文:
I would like to create a filter to replace values in a dataframe column based on a condition and also the values around it.
For exemple I would like to filter values and replace then with NaN if they are superior to 45 but also the value before and after it even if they are not meeting the condition:
df[i] = 10, 12, 25, 60, 32, 26, 23
In this exemple the filter should replace 60 by NaN and also the value before (25) and the value after (32).The result of the filter would be :
df[i] = 10, 12, NaN, NaN, NaN, 26, 23
So far I am using this line but it only replace value that meet the condition and not also values around:
df[i].where(df[i] <= 45, np.nan, inplace=True)
答案1
得分: 2
你可以将原始值与通过|
位运算链接的移位值进行比较,用于按位OR
操作:
m = df['i'].gt(45)
mask = m.shift(fill_value=False) | m.shift(-1, fill_value=False) | m
#替代解决方案,通过参数限制+1、-1的值
#mask = df['i'].where(m).ffill(limit=1).bfill(limit=1).notna()
df.loc[mask, 'i'] = np.nan
另一个用于一般掩码的想法(但类似上面的解决方案较慢):
mask = (df['i'].rolling(3, min_periods=1, center=True)
.apply(lambda x: (x > 45).any()).astype(bool))
df.loc[mask, 'i'] = np.nan
英文:
You can compare original with shifted values of mask chained by |
for bitwise OR
:
m = df['i'].gt(45)
mask = m.shift(fill_value=False) | m.shift(-1, fill_value=False) | m
#alternative solution +1, -1 value by parameter limit
#mask = df['i'].where(m).ffill(limit=1).bfill(limit=1).notna()
df.loc[mask, 'i'] = np.nan
Another idea for general mask (but slowier like solution above):
mask = (df['i'].rolling(3, min_periods=1, center=True)
.apply(lambda x: (x>45).any()).astype(bool))
df.loc[mask, 'i'] = np.nan
答案2
得分: 1
你可以使用布尔掩码来过滤数据框列并将值替换为 NaN。
mask = (df["col"] > 45)
df.loc[mask, "col"] = np.nan
在这之后,你可以使用掩码来将满足条件的值前后的值立即替换为 NaN。
df.loc[(mask.shift(1) | mask) | (mask.shift(-1)), "col"] = np.nan
英文:
You can use boolean mask to filter the dataframe column and replace the value with Nan.
mask = (df["col"] > 45)
df.loc[mask, "col"] = np.nan
After this you can use the mask to replace the values immediately before and after each value that met the condition with NaN as well.
df.loc[(mask.shift(1) | mask) | (mask.shift(-1)), "col"] = np.nan
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论