Skip to content Skip to sidebar Skip to footer

Unable To Convert Pandas Dataframe Column To Int Variable Type Using .astype(int) Method

I'm iterating through rows of a dataframe to extract values as follows but what I receive is always a float value and I'm not able to convert to int for both result['YEAR_TORONTO']

Solution 1:

Your approach to use astype() is right but it does work if you column contain nan. You could try to first split

result["YEAR_TORONTO"].astype(str).str.split('.', expand=True)[0].tolist()

And then separate then take it from there.

Alternatively

Result.loc[RESULT['TORONTO'].notnull(), 'x'] = result.loc[result['TORONTO'].notnull(), 'x'].apply(int)

Post a Comment for "Unable To Convert Pandas Dataframe Column To Int Variable Type Using .astype(int) Method"