A. Chandu A. Chandu - 6 months ago 23
Python Question

How to resolve this RDBMS Kind Join functionality in Pandas Dataframe?

Task1 SubTask SubTask_ID Pre_Task TaskType Pre_Task_ID
0 Get home work done SubTask1 SUB_1 SubTask3 ABC NaN
1 Get home work done SubTask2 SUB_2 SubTask7 CC NaN
2 Get home work done SubTask3 SUB_3 SubTask5 CC NaN
3 Get home work done SubTask4 SUB_4 NaN CC NaN
4 Get home work done SubTask5 SUB_5 NaN AB NaN
5 Get home work done SubTask6 SUB_6 NaN BCC NaN
6 Get home work done SubTask7 SUB_7 SubTask3 NaN NaN


In Above Dataframe i want to locate Pre_Task values in SubTask then if there is any match found , The Respective SubTask_ID should be write into to Pre_Task_ID.

Any Help ?? Thanks

Answer

Is this the type of join you are looking for?

df['Pre_Task_ID'] = df[['Pre_Task']].merge(df[['SubTask', 'SubTask_ID']],
                                             right_on='SubTask', left_on='Pre_Task', how='left')['SubTask_ID'].values

                Task1   SubTask SubTask_ID  Pre_Task TaskType Pre_Task_ID
0 Get home work  done  SubTask1      SUB_1  SubTask3      ABC       SUB_3
1 Get home work  done  SubTask2      SUB_2  SubTask7       CC       SUB_7
2 Get home work  done  SubTask3      SUB_3  SubTask5       CC       SUB_5
3 Get home work  done  SubTask4      SUB_4       NaN       CC         NaN
4 Get home work  done  SubTask5      SUB_5       NaN       AB         NaN
5 Get home work  done  SubTask6      SUB_6       NaN      BCC         NaN
6 Get home work  done  SubTask7      SUB_7  SubTask3      NaN       SUB_3
Comments