Counting the Pandas apply () function

I am trying to iterate through a DataFrame, and when the value changes, increment the counter, and then set a new column equal to that value. I can get this to work with a global counter, for example:

def change_ind(row):
    global prev_row
    global k

    if row['rep'] != prev_row:
        k = k+1
        prev_row = row['rep']
    return k

But when I try to pass the arguments to the apply function as shown below, it no longer works. It seems that it resets the values ​​of k, prev_row every time it runs on a new line. Is there a way to pass arguments to a function and get the result I'm looking for? Or is the best way to do this at all?

def change_ind(row, k, prev_row):    
    if row != prev_row:
        k = k+1
        prev_row = row
    return k
+1
source share
1 answer

, shift cumsum, , :

In [107]:
df = pd.DataFrame({'rep':[0,1,1,1,2,3,2,3,4,5,1]})
df

Out[107]:
    rep
0     0
1     1
2     1
3     1
4     2
5     3
6     2
7     3
8     4
9     5
10    1

In [108]:    
df['rep_f'] = (df['rep']!=df['rep'].shift()).cumsum()-1
df

Out[108]:
    rep  rep_f
0     0      0
1     1      1
2     1      1
3     1      1
4     2      2
5     3      3
6     2      4
7     3      5
8     4      6
9     5      7
10    1      8
+3

Source: https://habr.com/ru/post/1649780/


All Articles