Here is my example:
I first create dataframe and save it to file
import pandas as pd
df = pd.DataFrame({'col_1':[['a','b','s'], 23423]})
df.to_csv(r'C:\test.csv')
Then df.col_1[0]
returns ['a','b','s']
a list
Later I read it from file:
df_1 = pd.read_csv(r'C:\test.csv', quoting = 3, quotechar = '"')
Now df_1['col_1'][0]
returns "['a' 's']"
a string.
I would like to get list back. I am experimenting with different read_csv
settings, but so far no luck
You're not going to get the list back without a bit of work
Use literal_eval
to convert the lists
import ast
conv = dict(col_1=ast.literal_eval)
pd.read_csv(r'C:\test.csv', index_col=0, converters=conv).loc[0, 'col_1']
['a', 'b', 'c']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.