Fastest way to select rows where value of column of strings is in a set
<p>I have a <code>set</code> of email addresses that I've selected from one set of values. I'd like to subset a <code>pandas DataFrame</code> to include only rows where the <code>unique_id</code> column value is not contained in the set. Here's what I've done that is running very slow:</p>

<pre><code>signup_emails = set(online_signup.unique_id)
non_email_signup_event_emails = event_attendees[event_attendees.unique_id.apply(lambda x: x in signup_emails) == False].email
</code></pre>

<p>The table is several hundred thousand rows, but my computer just freezes up on this command, shows a high CPU load, and doesn't terminate even after long waits (20 minutes). How can I do this faster?</p>

<p>Use the <code>isin</code> method.</p>

<pre><code>event_attendees[event_attendees.isin(signup_emails)]
</code></pre>

<p>For not in the signup_emails, you can do</p>

<pre><code>event_attendees[event_attendees.isin(signup_emails) == False]
</code></pre>

python pandas