F1990
F1990

Reputation: 637

Float, object, string, datatype conversion

I have the following dataframe with these two columns. I want to sum those two columns and do: sumgroup1 = np.sum(dfgroup1[['StartEarliestDifference','LatestEndDifference']].values)

This results in the following error: unsupported operand type(s) for +: 'float' and 'str'

StartEarliestDifference LatestEndDifference
    27.0                           218
    5.0                             8
    2.0                             3

StartEarliestDifference = float64 , and LatestEndDifference = object

I tried to convert the object in a float, with the following line :

dfgroup1['LatestEndDifference'].convert_objects(convert_numeric=True)

Unfortunately the LatestEndDifference is not changed to a float and the sum results in the same error. What's wrong with my method?

Upvotes: 0

Views: 30

Answers (1)

joris
joris

Reputation: 139142

The change of convert_objects is not in-place, you have to reassign it:

dfgroup1['LatestEndDifference'] = dfgroup1['LatestEndDifference'].convert_objects(convert_numeric=True)

Upvotes: 2

Related Questions