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.
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']))