43zombiegit
43zombiegit

Reputation: 127

Find the index of the last true occurrence in a column by row

I have the following table format:

id bool
1 true
2 true
3 false
4 false
5 false
6 true

I'd like it so that I could get another column with the index of the last true occurrence in the bool column by row. If it's true in it's own row then return it's own id. It doesn't sound too hard using a for loop but I want it in a clean pandas format. I.e in this example I would get:

column = [1,2,2,2,2,6]

Upvotes: 1

Views: 238

Answers (3)

G.G
G.G

Reputation: 765

df1.assign(col1=np.where(df1.bool2,df1.id,pd.NA)).fillna(method='pad')

  id  bool2  col1
0   1   True     1
1   2   True     2
2   3  False     2
3   4  False     2
4   5  False     2
5   6   True     6

Upvotes: 0

BENY
BENY

Reputation: 323396

In your case do

df['new'] = df['id'].mul(df['bool']).cummax()
Out[344]: 
0    1
1    2
2    2
3    2
4    2
5    6
dtype: int64

Upvotes: 3

mozway
mozway

Reputation: 262634

IIUC, you can mask and ffill:

df['new'] = df['id'].where(df['bool']).ffill(downcast='infer')

output:

   id    bool  new
0    1   True    1
1    2   True    2
2    3  False    2
3    4  False    2
4    5  False    2
5    6   True    6

Upvotes: 4

Related Questions