I have a huge data-frame. How should I replace a range of values (-200, -100) with NaN?
I have a huge data-frame. How should I replace a range of values (-200, -100) with NaN?
You can use pd.DataFrame.mask
:
df.mask((df >= -200) & (df <= -100), inplace=True)
This method replaces elements identified by True
values in a Boolean array with a specified value, defaulting to NaN
if a value is not specified.
Equivalently, use pd.DataFrame.where
with the reverse condition:
df.where((df < -200) | (df > -100), inplace=True)
As with many methods, Pandas helpfully includes versions which work with series rather than an entire dataframe. So, for a column df['A']
, you can use pd.Series.mask
with pd.Series.between
:
df['A'].mask(df['A'].between(-200, -100), inplace=True)
For chaining, note inplace=False
by default, so you can also use:
df['A'] = df['A'].mask(df['A'].between(-200, -100))