Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pandas read list in list data type from csv file

Tags:

python

list

csv

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.

like image 487
Yiting Cai Avatar asked Jul 02 '26 01:07

Yiting Cai


1 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})
like image 143
Elisha Avatar answered Jul 04 '26 15:07

Elisha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!