简体   繁体   English

如何使用pandas iterrows()获得当前迭代的计数?

[英]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. 我想遍历一个DataFrame ,但是index ,我打通观察时iterrows()是在数据帧的指数。 Instead, I want it to be 0 , 1 , 2 , 3 ... 相反,我希望它是0123 ...

        for ind, row in last_10.iterrows():
            print('ind', ind)

This returns: 返回:

2345
2346
2347
...

I want it to be: 我希望它是:

0
1
2
...

Using enumerate 使用enumerate

for ind, row in enumerate(last_10.iterrows()):
    print('ind', ind)
    print('data',row[1]['columns'])

range create range创建

for ind in range(len(last_10)):
    print('ind', ind)
    print('data',df.iloc[ind]['columns'])
    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"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM