Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我可以检查数据帧索引是否结束吗?_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 我可以检查数据帧索引是否结束吗?

Python 我可以检查数据帧索引是否结束吗?,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我使用while循环在数据帧中显示数据 while True: last_id = get_last_id() res = df.iloc[last_id + 1] 当last_id结束数据并仍使用last_id+1时,显示错误 IndexError: single positional indexer is out-of-bounds 我可以检查数据帧索引中是否没有最后一个id+1,然后不显示任何内容吗?为什么不从最后一行中删除+1: while True: las

我使用while循环在数据帧中显示数据

while True:
    last_id = get_last_id()
    res = df.iloc[last_id + 1]
当last_id结束数据并仍使用last_id+1时,显示错误

IndexError: single positional indexer is out-of-bounds

我可以检查数据帧索引中是否没有最后一个id+1,然后不显示任何内容吗?

为什么不从最后一行中删除
+1

while True:
    last_id = get_last_id()
    res = df.iloc[last_id]

使用
if-else
数据帧的长度进行测试,因为按位置选择:

while True:
    last_id = get_last_id()
    if len(df) != last_id + 1:
        res = df.iloc[last_id + 1]
     else:
        print ('cannot select next value after last index')
另一个想法是使用
try except
语句:

while True:
    try:
        last_id = get_last_id()
        res = df.iloc[last_id + 1]
    except IndexError:
        print ('cannot select next value after last index')

这看起来非常不自然,除非绝对必要,否则在处理熊猫时不要使用循环。你能分享你节目的其余部分吗?我怀疑可能还有其他的非对称设计选择。我想自动将pandas中的最后一个值更新到数据库中。我的熊猫索引没有跳过。所以我认为最好检查db中的最后一个值,如果db中的最后一个值在pandas中不相等,那么检查pandas行并找到添加到db的下一行。为什么要使用循环?再一次,这是一个单一的、几乎从来都不是正确的解决方案。我正在试图找到解决这个问题的新方法。我有这样的数据帧0a-1,1a-3,2a-4,3a-6,数据帧总是会增加A-7,A-8。。。还有更多。我只想运行代码1次,将最后的数据添加到数据库中。这有点不清楚,你们能分享一个DataFrame的例子吗?我在提问之前知道这个答案,但这是结果。29 PDF-38,30 PDF-40,31 PDF-41,32 PDF-42,33 PDF-43,打印(df.iloc[last_idx])输出为PDF-41,如果循环输出+1,则仍显示PDF-41