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.
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})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With