Pandas Read.csv returns arrays as strings

Viewed 236

So I've been saving data as a .csv using pandas and when I load the .csv back as a df it turns my arrays into strings.

eg:

v = np.linspace( 0, 1, 10)
i = []

for j in V:
    itemp = *some calc*
    i = i.append(itemp)
###

df_data = pd.DataFrame({'Vsd_res':[v], 'Isd_res':[i]})
df_res = df_res.append(df_data)
df_res = df_res.reset_index(drop = True)

df_res.to_csv(res.csv)

This takes my two arrays and stores them in this format in my .csv:

"[-0.5        -0.48989899 -0.47979798 ...]","[-6.835167e-05, -6.683581e-05, -6.678757e-05...]"

Here I wonder why the Vsd is stored without commas but Isd is?

And then I load them back in with :

df_res = pd.read_csv('res.csv', index_col = 0)

What I get looks like:

     Vsd_res                                         Isd_res
0   [-0.5     -0.48989899     -0.47979798 ...]    [-6.835167e-05, -6.683581e-05, -6.678757e-05...]

what I want:

     Vsd_res                               Isd_res
0   [-0.5,-0.48989899,-0.47979798 ...]    [-6.835167e-05, -6.683581e-05, -6.678757e-05...]

This kind of picks it up how I want, just the data in each array is a str not a float, this applies to both V and Isd. Matplot cant plot them because a [ is present.

I've tried using eval in the convertors setting but its got an issue with the lack of commas I think.

I'd really appreciate some help on this. Thanks!

0 Answers
Related