Skip to main content
 首页 » 编程设计

python之遍历行,并控制计数 i++

2025年05月04日15qlqwjy

我正在寻找迭代行的方法,但仅对每 20 或 30 行值应用一些方法 所以像这样:

更新代码

for index, row in df.iterrows(), index=+20: 
     location= geolocator.reverse("%s, %s" % (row['lat'],row['long']),timeout=None) 
      row['location']=location.address 
      time.sleep(3) 
return df 

实际上我尽量减少请求的数量,否则我会遇到超时问题。这就是为什么我尝试遍历行,并且只对每 20 行或第 60 行应用请求函数(因为我有 7000 行),而不是通过应用 time.sleep 方法来加快进程

请您参考如下方法:

试试这个:

for index, row in enumerate(df): 
    if index % 20 == 0: 
        # do something