Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...