Python Select Specific Row and Column

Code4Days picture Code4Days · Aug 4, 2014 · Viewed 32.7k times · Source

I wish to select a specific row and column from a CSV file in python. If the value is blank, I want to perform one action, and if the value is not blank, I want to perform another action.

I think the code should look something like this:

inputFile = 'example.csv'
reader = csv.reader(inputFile, 'rb')
for row in reader:
    if row[5][6] == '':              ==>  (I mean select value the 5th row and 6th column)
        (do this)
    else:
        (do that)

Any help or guidance on this topic would be helpful -- I've done a similar task using lists; however, since the CSV file is raw, I don't know what to do.

Answer

bananafish picture bananafish · Aug 4, 2014

When you think CSV, think pandas.

import pandas as pd

df = pd.read_csv('path/to/csv')

if df.iloc[5, 6]:
    # do stuff
else
    # do some other stuff