Issue
prac = pd.DataFrame(
{"A": [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
"B": [0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0],
"DesiredResult": [0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0]}
)
newDf = prac[['A','B']].mask(prac==0)
newDf['buySell'] = newDf['A'].combine_first(newDf['B'])
newDf['buySell'].ffill(inplace=True)
newDf
this is what I have done/written so far:
- I utilized mask() and create "NaN" values of columns "A" and "B"
- next, I combined column "A" and "B" into a single column.
- This is the part where I'm having trouble as I am trying to use ffill() to based on the range of the 1's in both columns.
Not sure how to deal w/ the ffill() based on the 2 columns from here.
Solution
IIUC, you could use a reverse cummax
(or bfill
if you have NaNs in place of 0) per group based on A:
prac['buySell'] = (prac.loc[::-1, 'B']
.groupby(prac['A'].eq(1).cumsum())
.cummax()[::-1]
)
Output:
A B DesiredResult buySell
0 0 0 0 0
1 1 0 1 1
2 0 0 1 1
3 0 1 1 1
4 0 1 1 1
5 0 1 1 1
6 0 0 0 0
7 1 0 1 1
8 0 0 1 1
9 0 0 1 1
10 0 0 1 1
11 0 0 1 1
12 0 1 1 1
13 0 1 1 1
14 0 1 1 1
15 0 0 0 0
Alternative:
newDf = prac[['A','B']].mask(prac==0)
newDf['buySell'] = newDf['B'].groupby(newDf['A'].eq(1).cumsum()).bfill()
Output:
A B buySell
0 NaN NaN NaN
1 1.0 NaN 1.0
2 NaN NaN 1.0
3 NaN 1.0 1.0
4 NaN 1.0 1.0
5 NaN 1.0 1.0
6 NaN NaN NaN
7 1.0 NaN 1.0
8 NaN NaN 1.0
9 NaN NaN 1.0
10 NaN NaN 1.0
11 NaN NaN 1.0
12 NaN 1.0 1.0
13 NaN 1.0 1.0
14 NaN 1.0 1.0
15 NaN NaN NaN
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.