Pandas, 'nan' fields after changing column datatypes

Question:

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?

['22000.0',
 '0.0',
 '40000.0',
 '9.5',
 '0.0',
 '0.0',
 '28.0',
 'nan',
 'nan',
 'nan',
 'nan',
 'nan']
Asked By: The Dan

||

Answers:

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)
Answered By: Nuri Taş
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.