Home > Net >  Python pandas - ValueError: invalid literal for int() with base 10:
Python pandas - ValueError: invalid literal for int() with base 10:

Time:12-15

I have a dataframe with a column named year in object format. I want to convert object to int but I have this error :

ValueError: invalid literal for int() with base 10: '2021.0'

Here is my code : data_h_df['year'].astype(str).astype(int)

CodePudding user response:

Use:

data_h_df['year'].astype(float).astype(int)

CodePudding user response:

There is a lot of answers to this question you should search before asking first, the year key in your dict is a string so you need to convert it to float before converting to int

Example:

myDict = {"year": "2021.0"}
int(float(myDict["year"]))

Use:

data_h_df["year"].astype(float).astype(int)
  • Related