Issue
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]
Solution
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
Answered By - BENY
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.