Counting within Pandas apply() function

Chris Hedenberg

I'm trying to iterate through a DataFrame and when a value changes, increment a counter, then set a new column equal to that value. I'm able to get this to work using a global counter, like so:

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 arguments to the apply function, as below, it no longer works. It seems like it is resetting the values of k, prev_row each time it operates on a new row. Is there a way to pass arguments to the function and get the result I'm looking for? Or a better way to do this altogether?

def change_ind(row, k, prev_row):    
    if row != prev_row:
        k = k+1
        prev_row = row
    return k
EdChum

You can achieve the same thing using shift and cumsum this will be significantly faster than looping:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Counting within Pandas apply() function

From Dev

Using a shift() function within an apply function to compare rows in a Pandas Dataframe

From Dev

Why can't I apply shift from within a pandas function?

From Dev

Getting previous row values from within pandas apply() function

From Dev

Getting previous row values from within pandas apply() function

From Dev

Exit a function within *apply

From Dev

Counting number of times a function is called within a Class

From Dev

Counting up within a function of Javascript.

From Dev

the order within group apply function

From Dev

Apply function with args in pandas

From Dev

Alternative to apply function in pandas

From Dev

Apply function within each subset of a dataframe

From Dev

using kmeans within an apply function in r

From Dev

Counting length of another object property within object function -- JavaScript?

From Dev

Simple function counting values from a list within certain range

From Dev

Exception Handling in Pandas .apply() function

From Java

Apply ewm function on Pandas groupby

From Java

apply using a function with iloc pandas

From Dev

Speeding up Pandas apply function

From Dev

Pandas Groupby Apply Function to Level

From Dev

pandas apply function with arguments no lambda

From Dev

Apply Customize Cumulative Function to Pandas

From Dev

Pandas and apply function to match a string

From Dev

How to omit counting certain p tags within p tag counting function in wordpress

From Dev

How to nest quantile() function within apply() function in R or RStudio

From Dev

Conditional counting within groups

From Dev

Counting within a period of time

From Dev

How to retrieve changed values of previous rows while within apply at pandas?

From Dev

Optimizing pandas filter inside apply function

Related Related

  1. 1

    Counting within Pandas apply() function

  2. 2

    Using a shift() function within an apply function to compare rows in a Pandas Dataframe

  3. 3

    Why can't I apply shift from within a pandas function?

  4. 4

    Getting previous row values from within pandas apply() function

  5. 5

    Getting previous row values from within pandas apply() function

  6. 6

    Exit a function within *apply

  7. 7

    Counting number of times a function is called within a Class

  8. 8

    Counting up within a function of Javascript.

  9. 9

    the order within group apply function

  10. 10

    Apply function with args in pandas

  11. 11

    Alternative to apply function in pandas

  12. 12

    Apply function within each subset of a dataframe

  13. 13

    using kmeans within an apply function in r

  14. 14

    Counting length of another object property within object function -- JavaScript?

  15. 15

    Simple function counting values from a list within certain range

  16. 16

    Exception Handling in Pandas .apply() function

  17. 17

    Apply ewm function on Pandas groupby

  18. 18

    apply using a function with iloc pandas

  19. 19

    Speeding up Pandas apply function

  20. 20

    Pandas Groupby Apply Function to Level

  21. 21

    pandas apply function with arguments no lambda

  22. 22

    Apply Customize Cumulative Function to Pandas

  23. 23

    Pandas and apply function to match a string

  24. 24

    How to omit counting certain p tags within p tag counting function in wordpress

  25. 25

    How to nest quantile() function within apply() function in R or RStudio

  26. 26

    Conditional counting within groups

  27. 27

    Counting within a period of time

  28. 28

    How to retrieve changed values of previous rows while within apply at pandas?

  29. 29

    Optimizing pandas filter inside apply function

HotTag

Archive