1

I am trying to convert a column "value $" from datatype "Object" to "float" as this column would be involved in a numeric computation.

I initially replaced "$" in this column using:

 df['Value $'] = df['Value $'].replace({'\$': ''},regex=True)

Then convert it into numeric using:

 df['Value $'] = df['Value $'].astype(dtype=np.float64)

enter image description here

2 Answers 2

2

Probably it's because you should to replace the commas as well:

df['Value $'] = df['Value $'].replace({'\$|,': ''}, regex=True)
df['Value $'] = df['Value $'].astype(dtype=np.float64)
Sign up to request clarification or add additional context in comments.

Comments

1

You can just use this to replace.

df['Value $'] = df['Value $'].fillna(0.0).str.replace('[$,]', '').astype('float')

Comments

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.