Home > other >  How to load a pandas column from a csv file with lists as lists and not as strings
How to load a pandas column from a csv file with lists as lists and not as strings

Time:09-03

when I write a list in pandas when I read it, its dtype is string and not array, is there any way to write a column of list in such a way that be again array type when we read it?

Here is what I mean:

d=[['d',['A','B','C']],['p',['F','G']]]

df=pd.DataFrame(d)

df.to_csv('file.csv')

when I run the following code,

pd.read_csv('file.csv')['1'].values[0] 

the output is:

 "['A', 'B', 'C']"

but I want this:

  ['A', 'B', 'C']

CodePudding user response:

one solution would be to run it through enter image description here

type(df1["1"][0])

output:

enter image description here

  • Related