简体   繁体   English

检查熊猫 df.iterrows() 中的最后一行是否

[英]check if last row in pandas df.iterrows()

How can I check last row in Python pandas df.itterows() during its iteration?如何在迭代期间检查 Python pandas df.itterows() 中的最后一行?

My code :我的代码:

for index, row in df.iterrows():
...     # I want to check last row in df iterrows(). somelike row[0].tail(1)

Use

for i, (index, row) in enumerate(df.iterrows()):
    if i == len(df) - 1:
        pass

The pandas.DataFrame class has a subscriptable index attribute. pandas.DataFrame 类有一个可下标的index属性。 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:因此,可以在使用iterrows()针对 df.index 属性的最后一个值迭代行时检查行的索引,如下所示:

for idx,row in df.iterrows():
    if idx == df.index[-1]:
        print('This row is last')

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

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