Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dataframe : replace value and values around based on condition

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading