iterate over pandas dataframe using itertuples

前端 未结 3 1468
天命终不由人
天命终不由人 2021-02-03 23:33

I am iterating over a pandas dataframe using itertuples. I also want to capture the row number while iterating:

for row in df.itertuples():
    print row[\'name\         


        
3条回答
  •  抹茶落季
    2021-02-04 00:08

    For column names that aren't valid Python names, use:

    for row in df.itertuples(index=False):
        print(row[df.columns.get_loc('My nasty - column / name')])
    

    If you don't specify index=False, the column before the one named will be read.

提交回复
热议问题