Dean219 Dean219 - 1 year ago 168
Python Question

Access column using dictreader

The problem:

I have been having an issue trying to find the average of a column from a csv file using python's dictreader.

I have tried:

Accessing the columns like this using the column name, this works but the column name is required and im unsure how to loop over the reader.fieldnames in a way to construct a list from just each single column rather than mixing all columns data into the same list :

for r in reader:

Example of the loop

for i in reader.fieldnames:
for r in reader:

This is fine, however prints out 1 element from each column for each row. This makes it difficult to assemble a list of say all prices, all names etc as it would just rebuild the dictreader in list form.


How can i read just a single entire column from dictreader so i can access each column individually as a list and perform operations on it?

Note: so far i have tried appending each element using the loop, but results in a N size array with 4 elements in each row.

Answer Source

If you're fine looping over your file once for each column you want to read, just build a dict comprehension of list comprehensions:

columns = {fieldname: [row.get(fieldname) for row in reader] for fieldname in reader.fieldnames}

There's not really a better way to do it, just based on the nature of the file... csv's are a series of rows, turning them into columns is gonna be a little wasteful. You can tinker with this if you only want certain fieldnames extracted.

If you really need to only read the file once, though:

columns = {}
for row in reader:
    for fieldname in reader.fieldnames:
        columns.setdefault(fieldname, []).append(row.get(fieldname))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download