pythonpandas

How to solve Dataframe to_numeric Error (Python)?


I have dataframe that contains float numbers. I want to do numeric operations as sum mult. etc. . Columns types are object. So i have to change this columns into a numeric columns. I use to_numeric function but it gives me NaN as a result.

How can i solve this problem?

Code :

#import libraries
import pandas as pd

data = pd.read_csv('file.csv', engine='python', delimiter=';')

#change object columns into a numeric columnn
for i in data.columns :
    data[i] = pd.to_numeric(data[i], errors='coerce')

Dataframe

t0 (actual) t0  t0,lower    t0,upper
0   11861,6318726842    0   0   0
1   4761,43316  5709,1728515625 3776,725188260803   7939,908970830105
2   36,22841951973635   0   0   0
3   583,3716479196096   0   0   0
4   25087,16436661841   26040,7890625   21825,20941707611   31905,394350044822
....

Result :

    t0 (actual) t0  t0,lower    t0,upper
0   NaN NaN NaN NaN
1   NaN NaN NaN NaN
2   NaN NaN NaN NaN
3   NaN NaN NaN NaN
4   NaN NaN NaN NaN

Solution

  • I can't reproduce your error, but I assume you could add decimal="," to pd.read_csv.

    From the docs:

    decimal : str, default ‘.’
    Character to recognize as decimal point (e.g. use ‘,’ for European data).

    So your code would look like this:

    #import libraries
    import pandas as pd
    
    data = pd.read_csv('file.csv', engine='python', delimiter=';', decimal=",")
    
    #change object columns into a numeric columnn
    for i in data.columns :
        data[i] = pd.to_numeric(data[i], errors='coerce')