select identical entries in two pandas dataframe
<p>I have two dataframes. (a,b,c,d ) and (i,j,k) are the name columns dataframes</p>

<pre><code>df1 =

  a  b  c  d
  0  1  2  3
  0  1  2  3
  0  1  2  3

df2 =

  i  j  k  
  0  1  2  
  0  1  2  
  0  1  2  
</code></pre>

<p>I want to select the entries that df1 is df2
I want to obtain</p>

<pre><code> df1=   

  a  b  c  
  0  1  2  
  0  1  2  
  0  1  2  
</code></pre>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.isin.html" rel="nofollow"><code>isin</code></a> for compare <code>df1</code> with each column of <code>df2</code>:</p>

<pre><code>dfs = []
for i in range(len(df2.columns)):
    df = df1.isin(df2.iloc[:,i])
    dfs.append(df)
</code></pre>

<p>Then <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html" rel="nofollow"><code>concat</code></a> all mask together:</p>

<pre><code>mask = pd.concat(dfs).groupby(level=0).sum()
print (mask)
      a     b     c      d
0  True  True  True  False
1  True  True  True  False
2  True  True  True  False
</code></pre>

<p>Apply <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing" rel="nofollow"><code>boolean indexing</code></a>:</p>

<pre><code>print (df1.ix[:, mask.all()])
   a  b  c
0  0  1  2
1  0  1  2
2  0  1  2
</code></pre>

python pandas dataframe