0

I imported a csv file in python. The problem is that python read the entire dataframe I imported (csv file) as a string, hence I can't do any mathematical operation with it.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
banks= pd.read_csv("banche.csv", sep= ";", index_col="Date")
df=pd.DataFrame()
df["returns1"]=banks["bank1"]

I want to do some operations with the array "returns1" and then build a diagram, but this is impossibile because all the values are read as string. I don't know how to convert the array (df) from string to float. I tried with:

float_returns1 = df["returns1"].astype(np.float)
float_returns1= np.asarray(float_returns, dtype=float)

I always get this error:

ValueError: could not convert string to float: '0,002123'

This is my dataframe "banks":

              bank1     bank 2    bank 3
Date                                     
13/04/2021  0.002123  -0.001968  0.005972
14/04/2021 -0.011017  -0.011020  0.003078
15/04/2021 -0.003856  -0.011377 -0.002630
16/04/2021  0.022796   0.010678  0.009011
19/04/2021  0.029767   0.026908  0.006317
...              ...        ...       ...
04/04/2022  0.021379  -0.007598 -0.004751
05/04/2022 -0.002165  -0.028410 -0.025298
06/04/2022 -0.019530  -0.022501 -0.029285
07/04/2022  0.008853  -0.005622 -0.015437
08/04/2022  0.102377   0.030297  0.015577

[257 rows x 3 columns]

I need returns1 to be a float array

df
             returns1
Date                 
13/04/2021   0,002123
14/04/2021  -0,011017
15/04/2021  -0,003856
16/04/2021   0,022796
19/04/2021   0,029767
...               ...
04/04/2022   0,021379
05/04/2022  -0,002165
06/04/2022   -0,01953
07/04/2022   0,008853
08/04/2022   0,102377

[257 rows x 1 columns

0 Answers0