Reputation: 12461
How can I check whether I'm in the last row while iterating through rows using df.itterows()
?
My code:
for index, row in df.iterrows():
... # I want to check last row in df iterrows(). somelike row[0].tail(1)
Upvotes: 16
Views: 19732
Reputation: 336
The pandas.DataFrame class has a subscriptable index
attribute. So one can check the index of the row when iterating over the row using iterrows()
against the last value of the df.index attribute like so:
for idx,row in df.iterrows():
if idx == df.index[-1]:
print('This row is last')
Upvotes: 19
Reputation: 294298
Use
for i, (index, row) in enumerate(df.iterrows()):
if i == len(df) - 1:
pass
Upvotes: 20