Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new column based on other column values - conditional forward fill?

Have the following dataframe

d = {'c_1': [0,0,0,1,0,0,0,1,0,0,0,0],
     'c_2': [0,0,0,0,0,1,0,0,0,0,0,1]}

df = pd.DataFrame(d)

I want to create, another column 'f' that returns 1 when c_1 == 1 until c_2 == 1 in which case the value in 'f' will be 0

desired output as follows

    c_1 c_2 f
0   0   0   0
1   0   0   0
2   0   0   0
3   1   0   1
4   0   0   1
5   0   1   0
6   0   0   0
7   1   0   1
8   0   0   1
9   0   0   1
10  0   0   1
11  0   1   0

Thinking this requires some kind of conditional forward fill, looking at previous questions however havn't been able to arrive at desired output

edit: have come across a related scenario where inputs differ and current solutions do not work. Will confirm answered but appreciate any input on the below

d = {'c_1': [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0],
     'c_2': [1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]}

df = pd.DataFrame(d)

desired output as follows - same as before I want to create, another column 'f' that returns 1 when c_1 == 1 until c_2 == 1 in which case the value in 'f' will be 0

   c_1  c_2 f
0   0   1   0
1   0   1   0
2   0   1   0
3   0   0   0
4   0   0   0
5   0   0   0
6   1   0   1
7   0   0   1
8   0   1   0
9   0   1   0
10  0   1   0
11  0   1   0
12  0   1   0
13  0   0   0
14  0   0   0
15  0   0   0
16  1   0   1
17  0   0   1
18  1   0   1
19  1   0   1
20  0   0   1
21  0   0   1
22  0   0   1
23  0   0   1
24  0   1   0
like image 624
greent Avatar asked Nov 25 '25 15:11

greent


1 Answers

You can try:

df['f'] = df[['c_1','c_2']].sum(1).cumsum().mod(2)

print(df)

    c_1  c_2  f
0     0    0  0
1     0    0  0
2     0    0  0
3     1    0  1
4     0    0  1
5     0    1  0
6     0    0  0
7     1    0  1
8     0    0  1
9     0    0  1
10    0    0  1
11    0    1  0
like image 199
anky Avatar answered Nov 27 '25 05:11

anky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!