Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Columns Row-Wise for Partial String Match

My question is similar to this: How to check whether the content of Column A is contained in Column B using Python DataFrame?

Unfortunately, the chosen answer results in a nonetype error in my case.

I have a pandas dataframe in the following format:

id,text_1,text_2_compare
1,yyy,yy
2,yxy,xx
3,zzy,zy
4,zzy,x
5,xyx,yx

I would like to compare the columns to see if "text_2_compare" is contained in "text_1" and create an new indicator.

id,text_1,text_2_compare,match
1,yyy,yy,1
2,yxy,xx,0
3,zzy,zy,1
4,zzy,x,0
5,xyx,yx,1

Any tips or tricks (particularly a vectorized implementation) would be most appreciated!

like image 268
Pylander Avatar asked Jun 18 '26 11:06

Pylander


2 Answers

Building on @Onyambu's answer.

in can be used in place of re.findall()

df["match"] = df.apply(lambda v: int(v[2] in v[1]),axis=1)
print(df["match"]

Output:

0    1
1    0
2    1
3    0
4    1
like image 167
gaganso Avatar answered Jun 21 '26 01:06

gaganso


import re

df['compare_match']=df.apply(lambda v:len(re.findall(v[2],v[1])),axis=1)

df
   id text_1 text_2_compare  compare_match
0   1    yyy             yy              1
1   2    yxy             xx              0
2   3    zzy             zy              1
3   4    zzy              x              0
4   5    xyx             yx              1

EDIT:

I actually thought OP needed the number of times text_2_compared appeared in text_1, but on reading the question again, it seems OP just wants an indicator variable. Thus using v[2] in v[1] as done above by @gaganso is sufficient

like image 26
KU99 Avatar answered Jun 21 '26 03:06

KU99



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!