palpen
11/18/2016 - 5:04 PM

Function that converts a csv file to a Pandas dataframe and iterates through its rows

Function that converts a csv file to a Pandas dataframe and iterates through its rows

import pandas


def list_iterator(csvfile, *args):

    print "The headers of the csv file you want printed are ", args

    df = pandas.read_csv(csvfile)

    # iterate through rows
    for i in df.itertuples(index=False, name="namedTuple"):

        print [getattr(i, x) for x in args]


if __name__ == '__main__':

    list_iterator("vars2test.csv", "id")
    list_iterator("vars2test.csv", "id", "varname")