'How can I get the count of the current iteration with pandas iterrows()?
I am trying to iterate over a DataFrame, but the index, I get when looking through iterrows() is the index in the DataFrame. Instead, I want it to be 0, 1, 2, 3...
for ind, row in last_10.iterrows():
print('ind', ind)
This returns:
2345
2346
2347
...
I want it to be:
0
1
2
...
Solution 1:[1]
from itertools import tee
import pandas as pd
df = pd.DataFrame(['AA', 'BB', 'CC'], columns = ['value'])
def pair_iterate(iter):
first, second = tee(iter)
next(second, None)
return zip(first, second)
for (i1, row1), (i2, row2) in pair_iterate(df.iterrows()):
print(i1, i2, row1["value"], row2["value"])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | redhatvicky |
