Sometimes, we want to replace blank values (white space) with NaN in Python Pandas.
In this article, we’ll look at how to replace blank values (white space) with NaN in Python Pandas.
How to replace blank values (white space) with NaN in Python Pandas?
To replace blank values (white space) with NaN in Python Pandas, we can call replace
on the data frame.
For instance, we write
df = pd.DataFrame([
[-0.532681, 'foo', 0],
[1.490752, 'bar', 1],
[-1.387326, 'foo', 2],
[0.814772, 'baz', ' '],
[-0.222552, ' ', 4],
[-1.176781, 'qux', ' '],
], columns='A B C'.split(), index=pd.date_range('2000-01-01','2000-01-06'))
print(df.replace(r'^\s*$', np.nan, regex=True))
to create the df` data frame.
Then we replace all whitespace values with NaN by call replace
with the regex to match whitespaces, np.nan
and regex
set to True
.
Conclusion
To replace blank values (white space) with NaN in Python Pandas, we can call replace
on the data frame.