简体   繁体   中英

Is it possible to find common values in two dataframes using Python?

I have a dataframe df1 that is the list of e-mails of people that downloaded a certain e-book, and another dataframe df2 that is the e-mails of people that downloaded a different e-book.

I want to find the people that downloaded both e-books, or the common values between df1 and df2, using Python.

Is it possible to do that? How?

This was already discussed. Can you click on the below link

Find the common values in columns in Pandas dataframe

Assuming the two data frames as df1 and df2 with email column, you can do the following:

intersected_df = pd.merge(df1, df2, how='inner')

This data frame will have the values corresponding to emails found in df1 and df2

  1. Dump the emails from df1 into a set, in order to avoid duplicates.
  2. Dump the emails from df2 into a set, for the same reason.
  3. Find the intersection of these two sets, as such:
set1 = set(df1.Emails)`
set2 = set(df2.Emails)
common = set1.intersection(set2)```

I believe you should merge the two dataframes

merged = pd.merge(df1, df1, how='inner', on=['e-mails'])

and then drop the Nan values:

merged.dropna(inplace=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM