I have df as shown below:
x y z
0 a jj Nan
1 b ii mm
2 c kk nn
3 d ii NaN
4 e Nan oo
5 f jj mm
6 g Nan nn
Required Result:
x y z w
0 a jj Nan a
1 b ii mm a
2 c kk nn c
3 d ii NaN a
4 e Nan oo e
5 f jj mm a
6 g Nan nn c
Logics
take the union of column y and z: ii == jjsince in indices 1 and 5 they both have mmin column z
group this union: index 0,1,3,5 is a group, index 2,6 is another group
inside a group, randomly take one cell in column x and assign it to column w for the whole group
I have no information about this problem. Can someone help me?
EDITNOTE:
First, I posted a perfectly sorted column y and column z as follows:
x y z w
0 a ii NaN a
1 b ii mm a
2 c jj mm a
3 d jj Nan a
4 e kk nn e
5 f Nan nn e
6 g Nan oo g
In this case, piRSquared works perfectly.
EDITNOTE2:
Nickil Maveli's solution is great for my problem. However, I noted that the situation that the solution cannot cope with is as follows:
x y z
0 a ii mm
1 b ii nn
2 c jj nn
3 d jj oo
4 e kk oo
:
0 1 2 w
0 a ii mm a
1 b ii mm a
2 c jj nn c
3 d jj nn c
4 e kk oo e
w = ['a', 'a', 'a', 'a', 'a'].