Python pandas read list in list data type from csv file

Viewed 2852

I have a csv file, like this:

A       B
aaa     [['a1','b1'],['a2','b2']]
bbb     [['a3','b3']]

I tried to read this file into python. And I want the 'B' column in list data type.
I tried pd.read_csv('filename.csv'), what I got is "[['a1','b1'],['a2','b2']]" in str type, not in list type.
And I also tried pd.read_csv('filename.csv', converters={'B':list}), what I got here is like: [[, [',a','1,','b,'...., not the list I want.
Could you tell me how to read the list from a csv file directly? Thank you so much.

2 Answers

The issue here is caused by the serialization format of the list in column B. It is stored as a string representation of the list. In order to recover a python list back from the string, you can use eval as a converter:

df = pd.read_csv(source, converters={'B': eval})

to read list you should try this

import csv
with open('filename.csv', 'rb') as f:
    reader = csv.reader(f)
    your_list = list(reader)

print your_list
Related