Pandas convert non-null objects

2

After importing using urlib , Internet data, I can create a DataFrame (df) with two columns (Fecha y Valor) of the types:

Fecha    1563 non-null object, 
Valor    1563 non-null object.

With the statement df["Fecha"] = pd.to_datetime(df["Fecha"]) I can pass the values of the column Fecha , to the format Fecha 1563 non-null datetime64[ns]

Next I try to change the format of the column valor with the statement df["Valor"] = pd.to_numeric(df["Valor"]) and I get the error:

ValueError: Unable to parse string "185,130000" at position 0.

I try again with the df['Valor'] = df['Valor'].apply(np.float) statement and it gives me the error:

ValueError: Unable to parse string "185,130000" at position 0

What else could I do?

    
asked by efueyo 15.01.2018 в 13:02
source

1 answer

1

The problem is apparently the format of the string: the value 185,130000 can not be converted to any numeric value because it can not interpret the , as a decimal separator. A possible solution is to replace this character with . :

# Reemplazamos la , por .
df["Valor"] = df["Valor"].str.replace(",", ".")
# Y ahora sí deberíamos poder convertirlo a numérico
df["Valor"] = pd.to_numeric(df["Valor"])

On the other hand, if you are reading this data from a file csv using read_csv , it may be convenient to configure at that moment, the comma ( , ) as a decimal separator, using the parameter decimal=',' .

    
answered by 15.01.2018 / 15:39
source