0

I created a pd.Dataframe with 1 np.array in each cell of the columns "time series" and "time stamps", then I saved it as a csv and opened the csv in another script. My df looks like this:

enter image description here

The weird thing is that after opening the csv again, the two columns containing arrays are typecasted to strings.

So instead of something like this [1, 2, 3], every array looks like this "[1, 2, 3]".

Before saving and re-opening, everything was fine. I saved my df like this:

gss_epochs.to_csv(path_or_buf = path + "df.csv")

and opened it like this:

gss_epochs = pd.read_csv(path + "df.csv", sep = ",")

I haven't really found a solution for my problem online. Can anyone help me with this?

Thanks a lot in advance! Merle

Merle
  • 125
  • 1
  • 14
  • 1
    `csv` is inherently 2d - rows and columns. Have you looked at the `csv` file? The original lists are formatted as strings, and reloaded as such. – hpaulj Feb 04 '22 at 21:31
  • Ah okay, I didn't know that! Is there another way to save the dataframe (aka not as csv) so I don't have this problem? – Merle Feb 04 '22 at 21:33
  • 1
    Maybe you can convert the lists to additional columns or rows? That way, you'd get a more standard dataframe to work with. See e.g. [Splitting a list in a Pandas cell into multiple columns](https://stackoverflow.com/questions/40924332/splitting-a-list-in-a-pandas-cell-into-multiple-columns) – JohanC Feb 04 '22 at 22:37
  • 1
    One option might be to pickle your dataframe, then unpickle it when you want to use it with your other script. – Laurent Feb 05 '22 at 09:29
  • Okay I'll try that, thanks y'all! :-D – Merle Feb 05 '22 at 12:19

0 Answers0