I have two dataframe like,

df1

name value
A    1
B    2
A    3
D    4

df2

name class
A    1
B    3
E    4  

tried pd.merge(df1,df2,on='name',how='inner')

The output im getting is

name value class
A       1   1
A       3   1
B       2   3

my expected output is,

name  value
A       1   
A       3   
B       2

1 Answers

0
jezrael On Best Solutions

You can filter column for one column DataFrame:

df = pd.merge(df1,df2[['name']],on='name',how='inner')

What working same like:

df = pd.merge(df1,df2[['name']])

Another solution is use Series.isin with boolean indexing for filtering:

df = df1[df1['name'].isin(df2['name'])]