0

I have a dataset like this (with many more columns):

      FLAG__DC SEXECOND CRM_TAUX
0            N        M      0,9
1            N        M      0,9
2            N        M      1,2
3            O        M        1
4            N        M        1
5            N        M      0,9
6            O        M        1
7            N        M      0,9

I want to convert the column CRM_TAUX to Float... Please help!

I have tried this but doesn't work:

df['CRM_TAUX'] = df.CRM_TAUX.replace(',','.')
df['CRM_TAUX'] = df.CRM_TAUX.apply(pd.to_numeric)

This is the error I get (and many more):

Unable to parse string "1,2" at position 0

Thanks in advance!

2
  • Can you please show us the error you are getting? Commented May 28, 2019 at 23:39
  • Updated the question Commented May 28, 2019 at 23:40

1 Answer 1

1

Use str.replace

df.CRM_TAUX.str.replace(',' , '.')

Out[2246]:
0    0.9
1    0.9
2    1.2
3      1
4      1
5    0.9
6      1
7    0.9
Name: CRM_TAUX, dtype: object

Next, call pd.to_numeric on it should work

s = df.CRM_TAUX.str.replace(',' , '.')
df['CRM_TAUX'] = pd.to_numeric(s)


Out[2250]:
  FLAG__DC SEXECOND  CRM_TAUX
0        N        M       0.9
1        N        M       0.9
2        N        M       1.2
3        O        M       1.0
4        N        M       1.0
5        N        M       0.9
6        O        M       1.0
7        N        M       0.9
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm, also added errors='coerce' in pd.to_numeric() for extra perfection. 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.