I have a dataframe with empty cells and would like to replace these empty cells with NaN. A solution previously proposed at this forum works, but only if the cell contains a space:
df.replace(r'\s+',np.nan,regex=True)
This code does not work when the cell is empty. Has anyone a suggestion for a panda code to replace empty cells.
Replace NaN with Empty String using replace() We can replace the NaN with an empty string using df. replace() function. This function will replace an empty string inplace of the NaN value.
Operating on Null Values As we have seen, Pandas treats None and NaN as essentially interchangeable for indicating missing or null values.
I think the easiest thing here is to do the replace twice:
In [117]:
df = pd.DataFrame({'a':['',' ','asasd']})
df
Out[117]:
a
0
1
2 asasd
In [118]:
df.replace(r'\s+',np.nan,regex=True).replace('',np.nan)
Out[118]:
a
0 NaN
1 NaN
2 asasd
Both other answers do not take in account all characters in a string. This is better:
df.replace(r'\s+( +\.)|#',np.nan,regex=True).replace('',np.nan))
More docs on: Replacing blank values (white space) with NaN in pandas
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With