Home > Blockchain >  Problem with converting a string that contains numbers to a numbers data type
Problem with converting a string that contains numbers to a numbers data type

Time:11-20

Currently, I'm working with a column called 'amount' that contains transaction amounts. This column is from the string datatype and I would like to convert it to a number data type.

The problem I ran into was that the code I wrote to convert the string data type to numbers worked but the only problem is that when I removed the ',' in the code below and changed it to numbers, the decimals were added which causes extremely high values in my data. So, 100000,95 became 10000095. I used the following code to convert my string data type to numbers:

df["amount"] = df["amount"].str.replace(',', '')
df['amount'] = pd.to_numeric(df['amount'], errors='coerce') 

Can someone help me with this problem?

EDIT: Not all values contain decimals. I'm looking for a solution for only the values that contain a ','.

CodePudding user response:

You need repalce by comma if need floats:

df["amount"] = df["amount"].str.replace(',', '.')
  • Related