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

Time:05-21

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

CodePudding user response:

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()

CodePudding user response:

You can use int(),

data['ID'] = list(map(lambda x: int(x), data['ID']))
  • Related