comparing two Dataframe columns to check if they have same value in python

2024/11/15 20:25:56

I have two dataframes,

new1.Name       city0    sri won    chn1    pechi won  pune2    Ram won    mum0    pec won    keralanew3req
0   pec
1   mut

I tried,

mask=new1.Name.str.contains("|".join(new3.req.values.tolist()))
new1[mask]

I am getting,

 new1[mask]Name       city1  pechi won    pune0  pec won      kerala

As "pechi" contains "pec", it took this valu. but I want the exact match between the values not "contains"

my desired output is,

 new1[mask]Name       city0  pec won      kerala
Answer

You need \b that means "word boundary":

a = r'\b(' + "|".join(new3.req.values.tolist()) + r')\b'
print (a)
\b(pec|mut)\bmask=new1.Name.str.contains(a)
df = new1[mask]
print (df)Name    city
0  pec won  kerala
https://en.xdnf.cn/q/119616.html

Related Q&A

Input gravity forms entries in a database locally stores (created with python)

I hope you are all doing alright. Is it possible to connect a gform entry to a database created with Python and stored in my PC with a little variation of the following code? add_action("gform_af…

Cant get javascript generated html using python

Im trying to create a python script that automatically gets the content of a table on a webpage. I manage to have it to work on pure html page, but there is one website that gives me headache... The ht…

Python: Extract text from Word files in a url

Given the url containing a certain file, in this case a word document, read the contents of the document. I have seen several examples of how to extract text from local documents but not from a url. Wo…

Python3:Plot f(x,y), preferably using matplotlib

Is there a way, preferably using matplotlib, to plot a 2-variable function f(x,y) in python; Thank you, in advance.

Why does my cronjob not send the email from my script? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

How to delete unsaved tkinker label?

I made this program where I am putting labels on a grid without saving them in a variable. I do this because then I can for loop through a list of classes and get the data from each class in and add th…

Adjust every other row of a data frame

I would like to change every second row of my data frame.I have a df like this:Node | Feature | Indicator | Value | Class | Direction -------------------------------------------------------- 1 | …

Why is the list index out of range?

Im new at programing and Im trying to check a piece of code that keeps giving me this error: t[i] = t[i - 1] + dt IndexError: list index out of rangeThe code is the following: dt = 0.001t = [0] for i i…

Stopping a while loop mid-way - Python

What is the best way to stop a while loop in Python mid-way through the statement? Im aware of break but I thought using this would be bad practice.For example, in this code below, I only want the pro…

Click on element in dropdown with Selenium and Python

With Selenium and Chrome webdriver on MacOS need to click dropdown element. But always have an error that cant find. Have this html code on a page where it located:<select id="periodoExtrato&qu…