Python Forum

Full Version: changing format to Int
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

I am trying to change a format to int but I think it is having issues because the numbers have ,.

data['Imports_vfd'].astype(str).astype(int)
ValueError: invalid literal for int() with base 10: '7,255'

Anyone know how I can either remove the , form the column in the dataframe or convert to int with ,?

Thanks
If the dash in string represents the thousands separator, just get rid of it first:

int(data['Imports_vfd'].replace(",", ""))
Thanks, I tried that but it still won't let me convert to int.

data['Imports_vfd'].astype(str).astype(int(data['Imports_vfd'].replace(",", "")))
TypeError: cannot convert the series to <class 'int'>

Do you know what might be some of the common reasons for this?

Thanks
Oh, sorry, now I see that data['Imports_vfd'] has Pandas Series data type. So you must first convert it to the string, then do the replace and then convert it to the integer:

# converting Pandas Series to string
str_value = data['Imports_vfd'].astype(str)

# removing the dash from the string
str_value = str_value.replace(",", "")

# converting the string without the dash into the integer
int_value = int(str_value)
Or on one row:

int_value = int(data['Imports_vfd'].astype(str).replace(",", ""))