Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all values with NaN in the dataframe in pandas

Tags:

python

pandas

I have a small dataframe (df):

unique  a     b     c     d 
  0    None  None  None  None
  1    None  None  None  None
  2    None  0132  None  None
  3    None  None  None  0231
  4    None  None  None  None
  5    None  None  0143  None
  6    0121  None  None  None
  7    None  None  None  0432

I need to replace all values with NaN. I tried to apply df.fillna(np.NAN), but it does not change the value in cells, where there is a number. How do I make all the values have been replaced? Dataframe should look like this:

unique  a     b     c     d 
  0    NaN   NaN   NaN   NaN
  1    NaN   NaN   NaN   NaN
  2    NaN   NaN   NaN   NaN
  3    NaN   NaN   NaN   NaN
  4    NaN   NaN   NaN   NaN
  5    NaN   NaN   NaN   NaN
  6    NaN   NaN   NaN   NaN
  7    NaN   NaN   NaN   NaN
like image 864
yanadm Avatar asked Dec 05 '22 14:12

yanadm


2 Answers

Use loc to assign np.nan

df.loc[:] = np.nan

iloc also works

df.iloc[:] = np.nan
like image 117
piRSquared Avatar answered Feb 20 '23 23:02

piRSquared


Just to add to the pool, this also works:

df[:] = np.nan
like image 21
zipa Avatar answered Feb 20 '23 23:02

zipa