Home > Blockchain >  Pandas, converting columns to integer, invalid literal for int() with base 10
Pandas, converting columns to integer, invalid literal for int() with base 10

Time:11-26

.dtypes shows the "Amount" column is by default an object

So I tried this,

df['Amount'] = df['Amount'].astype(int)

I got this error,

ValueError: invalid literal for int() with base 10: '3,448.91'

CodePudding user response:

The issue is that your column contains a comma(,). First replace that with empty string, then convert the type to int.

df['Amount'] = df['Amount'].str.replace(',', '').astype(float)

If you want to round off the values and convert to int, do this:

df['Amount'] = df['Amount'].str.replace(',', '').astype(float).round().astype(int)
  • Related