1

I am having trouble properly incrementing a counter variable within a for loop.

I have a variable called flag, and I want to create a new variable called num based on the values in flag.

Input:

'flag'
FALSE
TRUE
TRUE
TRUE
FALSE
TRUE
FALSE
TRUE
TRUE

Desired output:

'flag' 'num'
FALSE   1
TRUE    1
TRUE    1
TRUE    1
FALSE   2
TRUE    2
FALSE   3
TRUE    3
TRUE    3

The goal is to start the num counter at 1 and keep that counter at 1 until another False instance occurs in the flag column. This would be continued until the end of the df.

My code:

def num(z):  
    i = 0
    for x in range (0,len(z)):
        if z['flg'] == False:
            return i + 1
        else:
            return i

df['num']=df.apply(num, axis=1)

I have tried incrementing the I counter in numerous places, but no luck...

Patrick
  • 5,526
  • 14
  • 64
  • 101
nakedbird226
  • 77
  • 2
  • 7
  • `z['flg']` always returns the same thing. Your loop does not affect it. – Ma0 Aug 28 '17 at 14:07
  • Also, if you meant to index `z` using `x`, iterate over `z.keys()`, not its length. And you shouldn't be returning in the loop. you should be doing `i = i+1` and returning at the end of the loop – user1999728 Aug 28 '17 at 14:09

2 Answers2

8
df['num'] = (~df['flag']).cumsum()

uses the fact that False = 0, True == 1

if df['num'] is not a column of True and False values, but a column of strings, I suggest you change that, or change the test to df['num'] = (df['flag'] == 'FALSE').cumsum()

Maarten Fabré
  • 6,938
  • 1
  • 17
  • 36
0

Here is your code in a corrected matter, but the solution by Maarten Fabré is nicer:

def num(z):
    i = 0
    numcol = []
    for flag in z:
        if not flag:
            i += 1
        numcol.append(i)
    return numcol

df['num'] = num(df['flag'])

At first I wanted to do it with a generator, but I did not get it to work with the pandas dataframe.

C.Fe.
  • 325
  • 3
  • 11