Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas, 'nan' fields after changing column datatypes

I am changing the datatype of a determined columns in pandas and I would like to keep the nan values as np.nan

After executing the following command:

df = df.astype({"raw_salary_from": str, "raw_salary_to": str})

What I get is that all the nan’s transformed into strings ‘nan’. How can I avoid that issue while transforming the datatype of the column?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

['22000.0',
 '0.0',
 '40000.0',
 '9.5',
 '0.0',
 '0.0',
 '28.0',
 'nan',
 'nan',
 'nan',
 'nan',
 'nan']

>Solution :

You can store the index of those nan’s first and then convert them to np.nan with mask. So:

idx_nan = df["raw_salary_from"].isna()
df["raw_salary_from"] = df["raw_salary_from"].astype(str).mask(idx_nan, np.nan)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading