Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change NaN to None in Pandas dataframe

Tags:

pandas

I try to replace Nan to None in pandas dataframe. It was working to use df.where(df.notnull(),None). Here is the thread for this method. Use None instead of np.nan for null values in pandas DataFrame

When I try to use the same method on another dataframe, it failed. The new dataframe is like below A NaN B C D E, the print out of the dataframe is like this:

     Unnamed: 1  Unnamed: 2 Unnamed: 3 Unnamed: 4 Unnamed: 5 Unnamed: 6  
0         A         NaN          B          C          D          E 
 

even when I use the working code run against the new dataframe, it failed. I just wondering is it is because in the excel, the cell format has to be certain type. Any suggestion on this?

like image 207
chun xu Avatar asked Dec 01 '25 06:12

chun xu


2 Answers

This always works for me

import numpy as np
df = df.replace({np.nan: None})

You can check this related question, Credit from here

like image 171
Burakudo Avatar answered Dec 10 '25 09:12

Burakudo


The problem is that I did not follow the format. The format I used that cause the problem was

df.where(df.notnull(), None)

If I wrote the code like this, there is no problem

df = df.where(df.notnull(), None)
like image 39
chun xu Avatar answered Dec 10 '25 10:12

chun xu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!