xman
xman

Reputation: 193

how to convert float to string excluding NaN in pandas within one line code?

I would like to convert a column of float value to string, following is my current way:

userdf['phone_num'] = userdf['phone_num'].apply(lambda x: "{:.0f}".format(x) if x is not None else x)

However, it also converts the NaN to string "nan" which is bad when I check the missing value in this column, any better idea?

Thanks!

Upvotes: 6

Views: 8369

Answers (1)

Arpit Solanki
Arpit Solanki

Reputation: 9931

I think you should compare Nan values instead of comparing None

userdf['phone_num'] = userdf['phone_num'].apply(lambda x: "{:.0f}".
                                          format(x) if not pd.isnull(x) else x)

Upvotes: 3

Related Questions