Home > Enterprise >  How do I fix TypeError: cannot convert the series to <class 'float'> when column in
How do I fix TypeError: cannot convert the series to <class 'float'> when column in

Time:10-22

I have a dataframe

dfooc
Name AddressId
XYZ  nan
ABC  <memory at 0x7f145136ca10>
HIJ  nan

How do I convert this AddressId column to float type? it is currently -

Name: AddressId, Length: 346498, dtype: object

I tried

dfooc['AddressId'] = int(float(dfooc['AddressId']))
raise TypeError(f"cannot convert the series to {converter}")

TypeError: cannot convert the series to <class 'float'>

I am converting this to float because if i let this be as it is, im unable to get this columns data into SQL server and im guessing its because SQL server doesnt like '<memory at 0x7f145136ca10>'

CodePudding user response:

You can do :

dfooc['AddressId'] = dfooc['AddressId'].astype('float', errors = 'ignore')

errors control raising of exceptions on invalid data for provided dtype according to the documentation, set to ignore, it will ssuppress exceptions and on error return original object (i.e. nan).

Or you can fillna first and then convert the series to float :

dfooc['AddressId'].fillna(0, inplace = True)
dfooc['AddressId'] = dfooc['AddressId'].astype('float')
  • Related