0

I want to fill NaN with null (as we get in database). Can we do that?I have tried fillna("") but this gives empty string.

Please guide me.

Regards

user15780176
  • 109
  • 8
  • 3
    Please clarify what you mean by "null (as we get in database)". In Python, "null" is represented by NoneType, but NaN is a different data type specific to float values... – xyzjayne Jun 09 '21 at 17:15
  • if a column is empty in database we get null , i am expecting pandas to give null instead of Nan – user15780176 Jun 09 '21 at 17:18
  • since Null is equal to None in python, I want to replace NaN to None – user15780176 Jun 09 '21 at 17:19
  • 1
    Tried it, it was giving an error ValueError: Must specify a fill 'value' or 'method'. – user15780176 Jun 09 '21 at 17:28
  • Got it, turns out you can't use None as a filler value in fillna or replace. What happens when you run ``df.isnull()``? For the entries where you have NaN values, is it giving you True of False? – xyzjayne Jun 09 '21 at 17:33
  • I am getting True – user15780176 Jun 09 '21 at 17:37
  • That means those values are already being interpreted as Null (by pandas definition at least). Can you specify where in your code or process these values are not being recognized as Null? – xyzjayne Jun 09 '21 at 17:39
  • 1
    I am trying to insert these values into mongoDB and it is getting inserted as NaN, I want them to insert as null – user15780176 Jun 09 '21 at 17:40
  • Got it. Here are a few answers that might help you: https://stackoverflow.com/questions/14162723/replacing-pandas-or-numpy-nan-with-a-none-to-use-with-mysqldb – xyzjayne Jun 09 '21 at 17:44
  • Thank you, it worked like a charm – user15780176 Jun 09 '21 at 17:51

0 Answers0