Char
Char

Reputation: 1735

How to filter Pandas rows by another Dataframe columns?

Say I have two Pandas dataframes like this:

df1 = pd.DataFrame([['asdf'], ['zxcv'], ['qwer'], ['hjkl']])
df2 = pd.DataFrame([['b','0'],['asdf','1'],['c','2'],['hjkl','3']])

How to I filter out df2 to only contain rows if it has a value from df1?

I want to get a dataframe like this from the operation

df3 = pd.DataFrame([['asdf','1'], ['hjkl', '3']])

Upvotes: 2

Views: 576

Answers (1)

DYZ
DYZ

Reputation: 57033

It's just a matter of merging:

pd.merge(df1,df2)
#      0  1
#0  asdf  1
#1  hjkl  3

Upvotes: 2

Related Questions