How do I fix TypeError: cannot convert the series to <class 'float'> when column in df has nan?

Question:

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>’

Asked By: Jay Janardhan

||

Answers:

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')
Answered By: Adrien Riaux
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.