Yiting Cai
Yiting Cai

Reputation: 31

Python pandas read list in list data type from csv file

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.

Upvotes: 3

Views: 3698

Answers (3)

rissorg
rissorg

Reputation: 1

I use pandas to handle file easily. If you can decide what gets in your file, you could also choose to store something like this:

A,B
aaa,a1 b1 + a2 b2
bbb,a3 b3

Then you can simply read your file and use the power of the split function which will:

  • split your string with the seperator of your choice (white space by default)
  • return the result in a list object.

For your second colum of the first row, you can simply run split once with the white space as a seperator and run it again with '+' as a seperator.

You can find more details on split here https://www.w3schools.com/python/ref_string_split.asp

import pandas

df = pandas.read_csv('my_file.csv')
for row in df.itertuples():
  my_list = row.A.split()

Upvotes: 0

Elisha
Elisha

Reputation: 23770

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})

Upvotes: 3

Argus Malware
Argus Malware

Reputation: 787

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

Upvotes: 0

Related Questions