my quantity dtype is object (from csv file) and I am trying to covert to int using below code:
df[x1] = df[x1].astype(str).astype(int)
It throws error as below:
ValueError: invalid literal for int() with base 10: '1,000.000'
Can anyone help me in this please?
CodePudding user response:
You need to remove ',' and '.'. You can use str.replace method to remove the comma and then cast the data to type float then to type int.
df[x1] = df[x1].str.replace(',','').astype(float).astype(int)
For example, for a Series such as
srs = pd.Series(['1,000.00','1'])
if you cast it to dtype int
srs.astype(int)
you get
ValueError: invalid literal for int() with base 10: '1,000.00'
Then if you remove the comma str.replace method and cast to dtype int
srs = srs.str.replace(',','')
srs.astype(int)
you get
ValueError: invalid literal for int() with base 10: '1000.00'
So you cast it to dtype float and to dtype int,
srs = srs.str.replace(',','').astype(float).astype(int)
you get the expected outcome:
0 1000
1 1
dtype: int32
CodePudding user response:
Actually int() function expects an integer string or a float, but not a float string. If a float string is given you need to convert it to float first then to int as:
int(float(userGuess))
So you have to convert it to float first and then to int.
