Yadollah shahryary dizaji Yadollah shahryary dizaji - 1 year ago 84
Python Question

How to get current and next value in Pandas DataFrame?

I have DataFrame like this:

index twp lat lng
0 ABINGTON 40.112603 -75.120223
1 AMBLER 40.157847 -75.220640
2 BERKS COUNTY 40.284542 -75.558035
3 BRIDGEPORT 40.104064 -75.343394

I want to get first value and second value then do something and then continue this loop, my process will be like this: (Index number )

to get values in index:


I done with irow() like this:

for i in range(0,distance.shape[0]):
last = distance.irow(i)
for j in range(i, distance.shape[0]):

But irow() is deprecated and I want to do with iloc or something comfortable with python 3.

Also, code above elements repeated with self I want do like example as I mentioned (index) above.

Answer Source

iloc will return the two rows if you passed them as a list:

from itertools import combinations

for i,j in combinations(distance.index, 2):
    distance[[i,j]]  # and do stuff with the 2 rows returned