简体   繁体   中英

Intersection of values in a common column of two dataframes- Pandas, Python3

I have 2 dataframes; dataframe main and dataframe mini, with exactly the same headers but with different values in them however there is some overlap. How would I get the duplicate values within a single column (eg Column 'Name').

Example:

dataframe main

Name    size    length
foo      1         2
foo2     3         4
foo3     5         6  
foo4     7         8

dataframe mini

Name     size     length
foox      60       70
foo3       3        4
fooy      50       60
foo4      7        8

psuedo code: intersect(column='Name', of='dataframe mini', against='dataframe main')

proposed out : (foo3,foo4)

You can use isin to mask the row values that are in another df:

In [52]:
main.loc[main['Name'].isin(mini['Name']), 'Name']

Out[52]:
2    foo3
3    foo4
Name: Name, dtype: object

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