python 将csv文件转换为Pandas数据帧并遍历其行的函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 将csv文件转换为Pandas数据帧并遍历其行的函数相关的知识,希望对你有一定的参考价值。

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")

以上是关于python 将csv文件转换为Pandas数据帧并遍历其行的函数的主要内容,如果未能解决你的问题,请参考以下文章