I have a dataframe like this
D_1 D_2 D_3 D_4
Boy
Boy play
Boy play car
Boy play chess
Boy play online
now I would like to have 3 more columns L_2, L_3 and L_4 where I can add up data data from the first three columns based on levels so that eventually I can have the result df as :
D_1 D_2 D_3 D_4 L_2 L_3 L_4
Boy boy|emp boy|emp|emp boy|emp|emp|emp
Boy play boy|play boy|play|emp boy|play|emp|emp
Boy play car boy|play boy|play|car boy|play|car|emp
Girl Girl|emp Girl|emp|emp Girl|emp|emp|emp
my solution from SQL looks like this
select *
, concat(D_1,"|",ifnull(D_2, "emp")) as L_2
, concat(D_1,"|",ifnull(D_2, "emp"), "|", ifnull(D_3, "emp")) as L_3
, concat(D_1,"|",ifnull(D_2, "emp"), "|", ifnull(D_3, "emp"), "|", ifnull(D_4, "emp")) as L_4
from abc
can anyone guide me how can i convert this in python scripting? Thanks in advance!