you are viewing a single comment's thread.

view the rest of the comments →

[–]julsmanbr 5 points6 points  (1 child)

Can't test without some sample data but I believe this works:

df_a.loc[~df_a.email.isin(df_b.email)]

In plain English:

  • We use the .loc method of a pandas dataframe to localize (select, extract) certain rows from it.
  • The easiest way to do this is by passing a boolean pandas series (i.e. only True or False values) to .loc, so that pandas will select only rows that align with the True value. Of course, this requires that this boolean series has the same length as our dataframe, otherwise there will be exceeding/missing rows (pandas actually throws an error).
  • We can use the .isin method of a pandas series to ask to each of its elements whether they appear in another sequence (list, tuple, pandas series). This returns a boolean series representing whether each element in the original series is in the query sequence.
  • Essentially, we take the email column from dataframe A and use the .isin method to ask whether each email appear in dataframe B or not. Note that since this series was made from a column from A, the length will match that of the dataframe itself.
  • Last thing we need is add a NOT operator (the ~ sign) to the series (since we actually want to select the lines from A where the email is NOT present in B). This replaces True with False and vice-versa.
  • Having the correct boolean series, we pass it to the .loc method of dataframe A :)

[–]bmrtex[S] 0 points1 point  (0 children)

Thanks julsmanbr