0

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
6
  • 2
    Something is wrong with the indention: There isn't any block following def funcA1(self):. Commented Dec 29, 2016 at 11:45
  • what kind of data structure is localiz? (how can you access elements with localiz[x,"CALC"]?) Commented Dec 29, 2016 at 11:58
  • @CodingLambdas: this is only and excerpt of my function. funcA1 is nested in funcA Commented Dec 30, 2016 at 14:24
  • @hiroprotagonist: localiz is the way I'm calling df.loc for the DataFrame (df.loc[row_indexer,column_indexer]) Commented Dec 30, 2016 at 14:24
  • @srWasabi Your code is still not valid. There has to be increased indention after a colon. Commented Dec 30, 2016 at 14:26

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

4 Comments

It doesn't quite answer the question, as @srWasabi isn't using a list, but probably a dictionary.
@CodingLambdas: ow you mention it: the syntax localiz[x,"CALC"] looks more like a numpy array than anything else. oh, no, actually; "CALC" would not be accepted as index. what kind of data structure is that?!
My guess is a dict with tuple objects as keys, as a[b, c] is equivalent to a[(b, c)]. Or it is a data structure by a library or one @srWasabi has built himself.
I'm using pandas for this and '"CALC"' is the column of my dataframe I use to check the end of each group of rows
1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.