Reading a csv file by column

199 Views Asked by At

I have a code to read csv file by row

import csv

with open('example.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        print(row)
        print(row[0])

But i want only selected columns what is the technique could anyone give me a script?

4

There are 4 best solutions below

2
On
import csv

with open('example.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    column_one = [row[0] for row in readCSV ]

Will give you list of values from the first column. That being said - you'll have to read the entire file anyway.

0
On

You can't do that, because files are written byte-by-byte to your filesystem. To know where one line ends, you will have to read all the line to detect the presence of a line-break character. There's no way around this in a CSV.

So you'll have to read all the file -- but you can choose which parts of each row you want to keep.

0
On

read_csv in pandas module can load a subset of columns. Assume you only want to load columns 1 and 3 in your .csv file.

import pandas as pd

usecols = [1, 3]
df = pd.read_csv('example.csv',usecols=usecols, sep=',')

Here is Doc for read_csv. In addition, if your file is big, you can read the file piece by piece by specifying chucksize in read_csv

0
On

I would definitely use pandas for that.

However, in plain python this one of the way to do it.

In this example I am extracting the content of row 3, column 4.

import csv

target_row = 3
target_col = 4

with open('yourfile.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile)
    n = 0
    for row in reader:
        if row == target_row:
            data = row.split()[target_col]
            break

print data