I have a multicolumns df with about 2000 rows. df look like this:
site le_cell le_id ca ca_id
1 101 1011 1 NAN NAN
2 101 1012 2 NAN NAN
3 101 1013 3 NAN NAN
4 110 1101 1 2 11
5 110 1102 2 2 12
6 110 1103 3 2 13
7 110 1104 11 2 1
8 110 1105 12 2 2
9 110 1106 13 2 3
Here's the problem. I need to create a new column, called 'part_id' and values gonna be: Groupby 'site', and if there is no 'ca' (ca = NAN) then 'part_id' is equal le_id (part_id=le_id). If has 'ca', than read 'ca_id' and value of 'part_id' is gonna be 1, 2, 3. 1 and 11 = 1, 2 and 12 = 2, 3 and 13 = 3. Desired output:
site le_cell le_id ca ca_id part_id
1 101 1011 1 NAN NAN 1
2 101 1012 2 NAN NAN 2
3 101 1013 3 NAN NAN 3
4 110 1101 1 2 11 1
5 110 1102 2 2 12 2
6 110 1103 3 2 13 3
7 110 1104 11 2 1 1
8 110 1105 12 2 2 2
9 110 1106 13 2 3 3
Just to mention, a can't just transform all le_id values from 11, 12, 13 to 1, 2, 3. So I need to go through 'ca' and match with 'le_cell' with the same 'le_id' as that 'ca_id'.
I've tried with converting to dict, but it's not gonna well, really have no idea how to start. At least, give me some hint.