我有一个要在Python中解决的数据处理问题。 我知道这样做的SQL方式,并且想知道如何在Python中完成同样的操作
将dataframe1中的两个列对匹配到dataframe2中的两个列对,并在找到匹配对时用新列更新数据框1。
IN SQL can be acheived by joining two tables (A,B)
WHERE ((A.COLUMN1=B.COLUMN3
AND A.COLUMN2=B.COLUMN4 ) OR ( A.COLUMN2=B.COLUMN3
AND A.COLUMN1=B.COLUMN4 ))
df1=pd.dataframe([{'column1':'APPLE','column2':'RED'},{'column1':'BANANA','column2':'YELLOW'},
{'column1':'GREEN','column2':'APPLE'},{'column1':'BERRY','column2':'BLUE'},{'column1':'CHERRY','column2':'RED'}],column=["column1","column2"])
df2=pd.dataframe([{'column3':'GREEN','column4':'LOLLIPOP'},{'column3':'APPLE','column4':'RED'},
{'column3':'YELLOW','column4':'BANANA'},{'column3':'BERRY','column4':'BLUE'},column=["column3","column4"])
df1
column1 column2
0 APPLE RED
1 BANANA YELLOW
2 GREEN APPLE
3 BERRY BLUE
4 CHERRY RED
df2
column3 Column4
0 GREEN LOLLIPOP
1 APPLE RED
2 YELLOW BANANA
3 BERRY BLUE
OUTPUT - df1
column1 column2 new_column
0 APPLE RED 1
1 BANANA YELLOW 1
2 GREEN APPLE 0
3 BERRY BLUE 1
4 CHERRY RED 0