Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a string value with NaN in pandas data frame - Python

Tags:

People also ask

How do I replace words with NaN pandas?

Use df. replace(np. nan,'',regex=True) method to replace all NaN values to an empty string in the Pandas DataFrame column.

How do I replace a string in a DataFrame in Python?

You can replace a string in the pandas DataFrame column by using replace(), str. replace() with lambda functions.


Do I have to replace the value? with NaN so you can invoke the .isnull () method. I have found several solutions but some errors are always returned. Suppose:

data = pd.DataFrame([[1,?,5],[?,?,4],[?,32.1,1]])

and if I try:

pd.data.replace('?', np.nan)

I have:

     0     1  2
0  1.0   NaN  5
1  NaN   NaN  4
2  NaN  32.1  1    

but data.isnull() returns:

       0      1      2
0  False  False  False
1  False  False  False
2  False  False  False

Why?