Issue
How the TRUE_MAX
column is calculated?
Input:
import pandas as pd
A=[28,30,15,25,24,13,31,19,20,11,19,21]
COND=[False,True,True,True,False,False,True,True,True,False,True,True]
df=pd.DataFrame({'A':A,'COND':COND})
Expected Output
A COND TRUE_MAX
0 28 FALSE
1 30 TRUE 30
2 15 TRUE 30
3 25 TRUE 30
4 24 FALSE
5 13 FALSE
6 31 TRUE 31
7 19 TRUE 31
8 20 TRUE 31
9 11 FALSE
10 19 TRUE 21
11 21 TRUE 21
Solution
You could try as follows:
import pandas as pd
A=[28,30,15,25,24,13,31,19,20,11,19,21]
COND=[False,True,True,True,False,False,True,True,True,False,True,True]
df=pd.DataFrame({'A':A,'COND':COND})
# splitting your data in groups: 1 through to 6
sequences = (df.COND != df.COND.shift()).cumsum()
# use only groups where df.COND == True and for each group get max in df.A
max_vals = df[df.COND].groupby(sequences)['A'].transform(max)
# assign to orig df
df['TRUE_MAX'] = max_vals
print(df)
A COND TRUE_MAX
0 28 False NaN
1 30 True 30.0
2 15 True 30.0
3 25 True 30.0
4 24 False NaN
5 13 False NaN
6 31 True 31.0
7 19 True 31.0
8 20 True 31.0
9 11 False NaN
10 19 True 21.0
11 21 True 21.0
Answered By - ouroboros1
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.