0

I'm trying to read a csv file with hundreds of float columns. Half of them have '.' as decimal mark, the others have ',' as decimal mark and none of them have any thousands separator so it would be helpful if one could set decimal parameter in pd.read_csv to ',' or '.' but it seems that Only length-1 decimal markers is supported for this parameter. Only one half of my columns are imported in dataframe with float dtype. The second half is Object dtype that must be treated separately to be converted to float.

>>> import pandas as pd
>>> df0 = pd.read_csv('example.csv')
>>> df0.head()
    col1   col2
0  123,2  12.02
1  22,15   1.50
>>> df0.dtypes
col1     object
col2    float64
dtype: object
>>> df1 = pd.read_csv('example.csv', decimal=',')
>>> df1.head()
     col1   col2
0  123.20  12.02
1   22.15    1.5
>>> df1.dtypes
col1    float64
col2     object
dtype: object

==> Is there any pythonesque way to import all columns as float and treat both '.' and ',' characters as decimal mark?

1
  • I think you need to replace ',' with '.' after reading the csv. I dont think regex can be applied to decimal parameter. Commented Sep 20, 2017 at 11:00

2 Answers 2

1

Before you read the file, use this:

with open("example.csv") as f:
    content = f.read()

content = content.replace('","','###')    #To prevent deleting required commas
content = content.replace(',','.')
content = content.replace('###','","')

with open("example.csv", "w") as f:
    content = f.write(content)
Sign up to request clarification or add additional context in comments.

Comments

0

You can select objects and convert them to float:

obj = df0.select_dtypes(include=['object']).apply(lambda x: x.apply(lambda y: float(y.replace(',', '.'))))
df0[obj.columns] = obj 

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.