如何解决Pandas Dataframe中的这种RDBMS种类连接功能?

时间:2016-05-18 13:08:08

标签: python 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

在Above Dataframe中我想在SubTask中找到Pre_Task值,然后如果找到任何匹配,则应该将相应的SubTask_ID写入Pre_Task_ID。

任何帮助?感谢

1 个答案:

答案 0 :(得分:1)

这是您要找的join类型吗?

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