8

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
1
  • df.fillna() kind of does the opposite of what you want. It replaces all NaN-values with another value. So df.fillna(np.NAN) replaces all NaN with NaN which makes no sense. Commented Jul 3, 2017 at 9:32

3 Answers 3

11

Use loc to assign np.nan

df.loc[:] = np.nan

iloc also works

df.iloc[:] = np.nan
Sign up to request clarification or add additional context in comments.

Comments

6

Just to add to the pool, this also works:

df[:] = np.nan

Comments

4

Use DataFrame constructor and pass index and columns names by df:

df = pd.DataFrame(columns=df.columns, index=df.index)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.