Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

pandas - How to merge in python based on two conditions?

I would like to create a join using a composite ID (car, ID) and if both match on the first df the use the test column value to create a new column

# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data1 = [['ford', 1010], ['chevy', 1515], ['toyota', 1515]] 
  
# Create the pandas DataFrame 
df_1 = pd.DataFrame(data1, columns = ['Car', 'ID']) 
  
data2 = [['ford', 1010, 'sat'], ['chevy', 1515, 'unsat'], ['toyota', 1515, 'sat']] 
  
# Create the pandas DataFrame 
df_2 = pd.DataFrame(data2, columns = ['Car', 'ID', 'Test']) 

I currently use merge for single column joins. But this yields incorrect info since different cars can have same IDs for test.

df_1_2 = pd.merge(df_1, df_2, on ='ID', how='left')
print(df_1_2)

While the answer I am looking for is some thing like this:

enter image description here

question from:https://stackoverflow.com/questions/66052952/how-to-merge-in-python-based-on-two-conditions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just do a merge without the on= keyword, like below:

df_1_2 = pd.merge(df_1, df_2)

#      Car    ID   Test
#0    ford  1010    sat
#1   chevy  1515  unsat
#2  toyota  1515    sat

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...