Sun
Sun

Reputation: 1955

iterate over pandas dataframe using itertuples

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?

Upvotes: 56

Views: 125897

Answers (4)

Kone
Kone

Reputation: 1

If you have a large data frame, (for example million rows), working with itertuples is much much faster then working with iterrows.

From my experience, working with both is pretty easy, you can easily access the values of a data frame.

Upvotes: 0

Chris
Chris

Reputation: 6392

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

for i, row in enumerate(df.itertuples(index=False)):
    print(str(i) + 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.

Upvotes: 13

Ashok Kumar Pant
Ashok Kumar Pant

Reputation: 562

for row in df.itertuples():
    print(getattr(row, 'Index'), getattr(row, 'name'))

Upvotes: 40

piRSquared
piRSquared

Reputation: 294288

When using itertuples you get a named tuple for every row. By default, you can access the index value for that row with row.Index.

If the index value isn't what you were looking for then you can use enumerate

for i, row in enumerate(df.itertuples(), 1):
    print(i, row.name)

enumerate takes the place of an ugly counter construct

Upvotes: 78

Related Questions