Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic way to copy a column from a CSV file

I am looking for a pythonic and concise way to select a column in a .csv file and store all cells of the column in, e.g., a list.

import csv    

with open("/path/to/file.csv","r") as csvfile:
    reader = csv.DictReader(csvfile, delimiter=";")
    # TODO: select column for key "foo"
    # TODO: select column for key "bar"
    # TODO:store "foo" data in list
    # TODO: store "bar" data in list
like image 347
clstaudt Avatar asked Jan 14 '23 06:01

clstaudt


1 Answers

It's straightforward to get columns out of DictReader row dicts in pure Python, and someone else is probably writing an answer to that effect right now, so instead of duplicating that effort I'll show how to do this in one of my favourite Python libraries for data manipulation, pandas:

>>> import pandas as pd
>>> df = pd.read_csv("somefile.csv", sep=";")
>>> df
   foo  bar      apple
0    1  100       pear
1    2  200     orange
2    3  300  tangerine
3    4  400      peach
>>> df["foo"]
0    1
1    2
2    3
3    4
Name: foo
>>> df["bar"]
0    100
1    200
2    300
3    400
Name: bar
>>> df["foo"] * df["bar"]
0     100
1     400
2     900
3    1600
>>> list(df["foo"] * df["bar"])
[100, 400, 900, 1600]

In the dark pre-pandas days I had my own hand-crafted library for this kind of data access. After about fifteen minutes with pandas a few years ago I tossed it..

like image 106
DSM Avatar answered Jan 21 '23 06:01

DSM