Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas dataframe replace blanks with NaN

Tags:

string

pandas

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.

like image 587
Wannes Dermauw Avatar asked May 22 '15 09:05

Wannes Dermauw


People also ask

How do I replace blank cells in pandas?

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.

Is null equal to NaN in pandas?

Operating on Null Values As we have seen, Pandas treats None and NaN as essentially interchangeable for indicating missing or null values.


2 Answers

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
like image 171
EdChum Avatar answered Sep 20 '22 11:09

EdChum


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

like image 31
Guido Avatar answered Sep 21 '22 11:09

Guido