user3561251
user3561251

Reputation: 143

Convert CSV column to list

Say I have a CSV file. It contains:

a,b,c
1,2,3
4,5,6
7,8,9

How can I turn the column labeled 'a' into a list, without hard-coding a row to it?

Originally, I was doing a readline for the first line a,b,c and then hard-coding a row to each variable. But then I thought: what if I wanted to do it with the possibility of the CSV file not being arranged the same way - say:

b,a,c
2,1,3
5,4,6
8,7,9

What's the best way to go about this?

Upvotes: 3

Views: 30667

Answers (3)

Hackaholic
Hackaholic

Reputation: 19733

you can use collections.defaultdict:

import collections
my_dict = collections.defaultdict(list)
with open('your_file') as f:
    header = next(f).strip().split(',')
    for x in f:
         x = x.strip().split(',')
         my_dict[header[0]].append(x[0])
         my_dict[header[1]].append(x[1])
         my_dict[header[2]].append(x[2])

Upvotes: 1

Andy
Andy

Reputation: 50540

You can do this using the pandas library:

import pandas

data = pandas.read_csv("test.csv", header=0)
col_a = list(data.a)
col_b = list(data.b)
col_c = list(data.c)

The values in col_a, col_b and col_c are as follows:

[1, 4, 7]
[2, 5, 8]
[3, 6, 9]

Upvotes: 9

schesis
schesis

Reputation: 59108

csv.DictReader turns each row of your CSV file into a dictionary with the column headers as keys. For your case (assuming the two examples in your question are named abc.csv and bac.csv respectively), you could use it like this:

from csv import DictReader

with open("abc.csv") as f:
    a1 = [row["a"] for row in DictReader(f)]

with open("bac.csv") as f:
    a2 = [row["a"] for row in DictReader(f)]

# a1 == a2 == ['1', '4', '7']

Upvotes: 8

Related Questions