Remove columns from data frame with hashing
<p>Given two pandas dataframes:</p>

<pre><code>df1 = pd.read_csv(file1, names=['col1','col2','col3'])
df2 = pd.read_csv(file2, names=['col1','col2','col3'])
</code></pre>

<p>I'd like to remove all the rows in df2 where the values of either <code>col1</code> or <code>col2</code> (or both) do not exist in df1. </p>

<p>Doing the following:</p>

<pre><code>df2 = df2[(df2['col1'] in set(df1['col1'])) &amp; (df2['col2'] in set(df1['col2']))]
</code></pre>

<p>yields:</p>

<blockquote>
  <p>TypeError: 'Series' objects are mutable, thus they cannot be hashed</p>
</blockquote>

<p>I think you can try <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.isin.html" rel="nofollow"><code>isin</code></a>:</p>

<pre><code>df2 = df2[(df2['col1'].isin(df1['col1'])) &amp; (df2['col2'].isin(df1['col2']))]

df1 = pd.DataFrame({'col1':[1,2,3,3],
                    'col2':[4,5,6,2],
                    'col3':[7,8,9,5]})

print (df1)
   col1  col2  col3
0     1     4     7
1     2     5     8
2     3     6     9
3     3     2     5

df2 = pd.DataFrame({'col1':[1,2,3,5],
                    'col2':[4,7,4,1],
                    'col3':[7,8,9,1]})

print (df2)
   col1  col2  col3
0     1     4     7
1     2     7     8
2     3     4     9
3     5     1     1

df2 = df2[(df2['col1'].isin(df1['col1'])) &amp; (df2['col2'].isin(df1['col2'].unique()))]
print (df2)
   col1  col2  col3
0     1     4     7
2     3     4     9
</code></pre>

<p>Another solution is <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.merge.html" rel="nofollow"><code>merge</code></a>, because inner join (<code>how='inner'</code>) is by default, but it works only for values with same position in both <code>DataFrames</code>:</p>

<pre><code>print (pd.merge(df1, df2))
   col1  col2  col3
0     1     4     7
</code></pre>

python pandas indexing merge multiple-columns