简体   繁体   中英

Python loop with next row values

Is there a way to make my loop work with no errors because there is no next value? Or not to use a foor loop for this at all?

Inside this function below I have another function with a for loop:

def funcA(self,perc,bloc):
    def funcA1(self):
        maxIndex = len(self)
        localiz = self.loc
        for x in range(0,maxIndex-1):
            if localiz[x,bloc] == localiz[x+1,bloc]:
                localiz[x,"CALC"] = True
            else:
                localiz[x,"CALC"]= False
        return self

I got it working by creating first the column "CALC" with False because the last line of my df will always be False . But surely there is a better way.

EDIT I'm basically using pandas and numpy for this code.

The bloc that i'm using in the function is the ID column The data structure I'm working with is like this:

ID   NUMBER
2    100
2    150
3    500
4    100
4    200
4    250

And the expected results are:

ID   NUMBER   CALC
2    100      True
2    150      False
3    500      False
4    100      True
4    200      True
4    250      False

a pythonic way is this:

lst = [char for char in 'abcdef']
print(lst)
for i, (cur, nxt) in enumerate(zip(lst, lst[1:])):
    print(i, cur, nxt)

just note that cur will only run to the second-to-last element of lst .

this will print:

['a', 'b', 'c', 'd', 'e', 'f']
0 a b
1 b c
2 c d
3 d e
4 e f

i is the index in lst of the cur element.

lst[1:] creates a new list excluding the first element. if your lists are very long you may consider replaicing that part with islice ; that way no additional copy is made.


this also works if your arr is an n-dimensional numpy array:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], np.int32)
print(arr)
for i, (cur, nxt) in enumerate(zip(arr, arr[1:])):
    print(i, cur, nxt)

with ouput:

[[1 2 3]
 [4 5 6]
 [7 8 9]]
0 [1 2 3] [4 5 6]
1 [4 5 6] [7 8 9]

Because I'm not familiar with this vector-style solution that numpy gives us, I think I couldn't make the most of the proposed solution that was given.

I did find a way to overcome the loop I was using though:

def funcA(self,perc,bloc):
    def new_funcA1(self):

        df = self[[bloc]]
        self['shift'] = df.shift(-1)
        self['CALC'] = self[bloc] == self['shift']
        self.drop('shift', axis=1, inplace=True)
        return self

With pandas.DataFrame.shift(-1) the last row will return NaN. This way I don't have to make any adjustments for the first or last row and I got rid of the loop!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM