Lets say I have a dataframe.
| ID | B. | C. | D. | E |
|---|---|---|---|---|
| 1. | b1_main | null | d_value | e_value |
| 2. | b2_main | null | null | e_value |
The logic that I would want to apply concat value in B column to either value in C, D or E column. However, C will always take the first priority to concat with value in B column, if value in C column is null then it will only proceed to concat value in D column and proceed to E column if value in D column is also null.
Desired Output
| ID | B. | C. | D. | E |
|---|---|---|---|---|
| 1. | b1_main, d_value | null | d_value | e_value |
| 2. | b2_main, e_value | null | null | e_value |
The code that i tried is below however it will concat all the values in C,D and E and remove null value.
df['B'] = pb6_branded[['B','C', 'D', 'E']].apply(lambda x: ','.join(x.dropna()), axis=1)
Thank you.