How do I filter a dataframe based on complicated conditions?

Question:

Right now my dataframes look like this (I simplified it cause the original has hundreds of rows)

import pandas as pd

Winner=[[1938,"Italy"],[1950,"Uruguay"],[2014,"Germany"]]
df=pd.DataFrame(Winner, columns=['Year', 'Winner'])
print(df)

MatchB=[[1938,"Germany",1.0],[1938,"Germany",2.0],[1938,"Brazil",1.0],[1950,"Italy",2.0],[1950,"Spain",2.0],[1950,"Spain",1.0],[1950,"Spain",1.0],[1950,"Brazil",1.0],
[2014,"Italy",2.0],[2014,"Spain",3.0],[2014,"Germany",1.0]]
df2B=pd.DataFrame(MatchB, columns=['Year', 'Away Team Name','Away Team Goals'])
df2B

I would like to filter df2B so that I will have the rows where the "Year" and "Away Team Name" match df:

Filtered List (Simplified)

I check google but can’t find anything useful

Asked By: Chery

||

Answers:

You can merge.

df = pd.merge(left=df, right=df2B, left_on=["Year", "Winner"], right_on=["Year", "Away Team Name"])
print(df)

Output:

   Year   Winner Away Team Name  Away Team Goals
0  2014  Germany        Germany              1.0
Answered By: Jason Baker
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.