+1 vote
in Programming Languages by (59.8k points)
There are some NaN values in a column of a dataframe. How can I replace them with ' ' (a blank space)?

1 Answer

+1 vote
by (353k points)
selected by
 
Best answer

You can apply the fillna() method with ' ' as the argument to the specific column that contains NaN. It will replace NaN with ' '.

Here is an example:

>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame({'a':[1,2,3,4], 'b':[11,12,13,14], 'c':[21,np.nan,23,np.nan]})
>>> df
   a   b     c
0  1  11  21.0
1  2  12   NaN
2  3  13  23.0
3  4  14   NaN
>>> df['c']=df['c'].fillna(' ')
>>> df
   a   b     c
0  1  11  21.0
1  2  12      
2  3  13  23.0
3  4  14      
>>>


...