Reputation:
I'm looking for method, that iterates over the rows, but apply some method only for every 20th or 30th row values so something like:
UPDATED CODE
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
Actually I try to minimize the number of requests, cause otherwise I have the timeout issue. That's why I tried iterate over the rows, and apply the function of request only for every 20th or 60th row (cause I have 7000 rows) and not to speed the process by applying the time.sleep method
Upvotes: 1
Views: 1241
Reputation: 394399
Why not just slice the df using iloc
and a step param:
Example:
In [120]:
df = pd.DataFrame({'c':np.random.randn(30)})
df
Out[120]:
c
0 -0.737805
1 1.158012
2 -0.348384
3 0.044989
4 0.962584
5 2.041479
6 1.376785
7 0.208565
8 -1.535244
9 0.389831
10 0.049862
11 -0.142717
12 -0.794087
13 1.316492
14 0.182952
15 0.850953
16 0.015589
17 0.062692
18 -1.551303
19 0.937899
20 0.583003
21 -0.612411
22 0.762307
23 -0.682298
24 -0.897314
25 -0.101144
26 -0.617573
27 -2.168498
28 0.631021
29 -1.592888
In [121]:
df['c'].iloc[::5] = 0
df
Out[121]:
c
0 0.000000
1 1.158012
2 -0.348384
3 0.044989
4 0.962584
5 0.000000
6 1.376785
7 0.208565
8 -1.535244
9 0.389831
10 0.000000
11 -0.142717
12 -0.794087
13 1.316492
14 0.182952
15 0.000000
16 0.015589
17 0.062692
18 -1.551303
19 0.937899
20 0.000000
21 -0.612411
22 0.762307
23 -0.682298
24 -0.897314
25 0.000000
26 -0.617573
27 -2.168498
28 0.631021
29 -1.592888
This will be much faster than iterating over every row
So in your case:
df['C'].iloc[::20] = some_function()
should work
Upvotes: 0
Reputation: 49330
Just use enumerate
and the modulus operator:
for index, row in enumerate(df.iterrows()):
if not index%20:
row['C']=some_function()
return df
I took the return
out of the loop so that the loop wouldn't end after one iteration.
Upvotes: 2
Reputation: 8998
Try this:
for index, row in enumerate(df):
if index % 20 == 0:
# do something
Upvotes: 4