M ob
M ob

Reputation: 97

Converting a column to Numeric but it still shows as float with .0 value

I am trying to convert a column to int, so i don't have 0 in front of the number and .0 after the number. this is what i am using

data['ID'] = pd.to_numeric(data['ID'], errors='coerce')

but it is not converting the column type

here is image what it looks like.

enter image description here

Please if you can help

Upvotes: 2

Views: 310

Answers (1)

mozway
mozway

Reputation: 260380

This is due to the NaNs that are of float type.

You can use fillna to make the NaNs 0:

data['ID'] = pd.to_numeric(data['ID'], errors='coerce').fillna(0, downcast='infer')

Or to keep the NaN as the new integer NA, use convert_dtypes:

data['ID'] = pd.to_numeric(data['ID'], errors='coerce').convert_dtypes()

Upvotes: 1

Related Questions