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']
Expected output :
1 larry
2 barry
3 michael
1, 2, 3 are row numbers. I want to avoid using a counter and getting the row number. Is there an easy way to achieve this using pandas?
enumerate- a common pattern in Python for these cases - seems weird. I would use it. Otherwisedf.reset_index()will bring a 0 based index so the row number will be the index you iterate for a given line +1iterrowslike in this SO post