1

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'))

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 ?

1
  • I'm able to save list column as it is in csv. My pandas version: '1.2.3'. What version are you on? Commented Feb 20, 2022 at 5:35

4 Answers 4

2

Use JSON or HDF file format instead of CSV. CSV file format is really inconvenient for storing a list or a collection of objects.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

I think Anurag's suggestion is very good. But just in case you want to keep things the way it is, this will do the job

import json
df['col_list'] = df['col_list'].apply(json.loads)

This would work better if you had converted col_list into JSON text before pd.to_csv by

df['col_list'] = df['col_list'].apply(json.dumps)

Comments

0

Consider saving it as an excel file, if that's an option.

df.to_excel('my_file.xlsx',index=False)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.