Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

saving and loading list values?

I have a dataframe where one of the columns contains a list of values:

example:
type(df['col_list'].values[0]) = list

I saved this dataframe as csv file (df.to_csv('my_file.csv'))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When I load the dataframe (df = pd.read_csv('my_file.csv'))
the column which contains list of values change to string type:
type(df['col_list'].values[0]) = str

When converting to list (list(df['col_list'].values[0]) I’m getting list of characters instead of list of values.

How can I save/load dataframe which one of it’s columns contains list of values ?

>Solution :

This is due to the table being saved as CSV and serializing the values of the list. The csv format is unable to save the list object as it is. Try saving in another format df.to_pickle('test.df'). You can then read this back into a dataframe with read_pickle

Read more on saving to pickle here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading