0

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 ','.

1 Answer 1

2

You need repalce by comma if need floats:

df["amount"] = df["amount"].str.replace(',', '.')
Sign up to request clarification or add additional context in comments.

1 Comment

This was the thing I was looking for, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.