Yadollah shahryary dizaji Yadollah shahryary dizaji - 1 month ago 6
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:

(0,1)
(0,2)
(0,3)
(1,2)
(1,3)
(2,3)


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]):
print(last[0])
nex=distance.irow(j)
print(nex[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

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
Comments