Merge two dataframes based on a column

2024/10/13 23:16:06

I want to compare name column in two dataframes df1 and df2 , output the matching rows from dataframe df1 and store the result in new dataframe df3. How do i do this in Pandas ?

df1

place name qty unit
NY    Tom   2  10
TK    Ron   3  15
Lon   Don   5  90
Hk    Sam   4  49

df2

place name price 
PH    Tom   7    
TK    Ron   5    

Result:

df3

place name qty unit
NY    Tom   2  10
TK    Ron   3  15
Answer

Option 1

Using df.isin:

In [1362]: df1[df1.name.isin(df2.name)]
Out[1362]: place name  qty  unit
0    NY  Tom    2    10
1    TK  Ron    3    15

Option 2

Performing an inner-join with df.merge:

In [1365]: df1.merge(df2.name.to_frame())
Out[1365]: place name  qty  unit
0    NY  Tom    2    10
1    TK  Ron    3    15

Option 3

Using df.eq:

In [1374]: df1[df1.name.eq(df2.name)]
Out[1374]: place name  qty  unit
0    NY  Tom    2    10
1    TK  Ron    3    15
https://en.xdnf.cn/q/118023.html

Related Q&A

Python/Pandas - building a new column based in columns comparison

I have this dataframe:df:CNPJ Revenues 2016 Revenues 2015 Revenues 2014 0 01.637.895/0001-32 R$ 12.696.658 NaN R$ 10.848.213 1 02.916.265/0001-60 …

Present blank screen, wait for key press -- how?

lo,I am currently trying to code a simple routine for an experiment we are planning to run. The experiment starts by entering a subject number and creating a bunch of files. I got that part working. Ne…

Images appearing in all but 1 flask page

I am creating a web app in flask, python, mysql. When viewing every other page on my website my images load, but when viewing one specific page, I cant get any images to load using already working code…

Python: Write nested list objects to csv file

Im trying to write data from a list of lists to a csv file. This is a simplified version of what I haveclass Point(object): def __init__(self, weight, height):self.weight = weightself.height = heightde…

Entire module is not detected by __init__.py

I have a relatively small python program which is setup like thisRoot--Network--DTOLots of py files which contain classes.Other py files in the projectBoth in the Network and the DTO folder there is an…

Python output to terminal during ssh login

I have been looking everywhere for this and have not found a solution. I am using python 2.5.1 on an Apple iPod and I am trying to connect to a host with SSH. First I start off with import os. Next I o…

Unable to find SIFT or xfeatures2d in OpenCV Python [duplicate]

This question already has answers here:PyCharm: Installation of non-free OpenCV modules for operations like SIFT, SURF(2 answers)Closed 6 years ago.I recently switch back to python for facial detection…

Django paginate for django 2

I need to use pagination to a Django list but I couldnt find any help online,, only old docs from Django version 1.3 here are my files : views.pydef home(request):all_dress = Item.objects.all().filter(…

AttributeError Button object has no attribute scrlFBtn

from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.core.window import Window from kivy.uix.scrollview import ScrollView from kivy.effects.scrol…

How to set interpreter of WinPython as the vim default python interpreter?

I use a Python distribution named WinPython. Now I want my vim to use the python interpreter in WinPython as its default interpreter. I tried add the F:\WinPython\python-2.7.3.amd64 into my windows env…