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
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.
Comments