Home > Software design >  How to convert data frame types Object to Integer / float?
How to convert data frame types Object to Integer / float?

Time:12-05

Hi I do exercise with data from Telco Costumer Churn, I want to convert column "TotalCharges" data types from object to integer but when I run

df_1['TotalCharges'].astype(str).astype(int)

always end with error ValueError: invalid literal for int() with base 10: '29.85'

CodePudding user response:

You can use float() to convert your string to a float, then int() to get the (truncated) integer or round() if you wanted the number rounded:

int(float(df_1['TotalCharges']))

CodePudding user response:

You should first convert to float then convert to int

df_1['TotalCharges'].astype(float).astype(int)

if you consider to be ceil or floor can use math.ceil() or math.floor

  • Related