简体   繁体   中英

Python - Is one string column in another?

I have a pandas dataframe with two columns. I need to determine if the string value from one column is in the string value of another column. The second column could be a 'single value' like 'value1' or it could be multiple items separated by a '/' in the string, like this: 'value1/value2/value3'.

For each row, I need to determine if the string is present in the other string in the same row, so that 'value1' in 'value1/value2/value3' would evaluate to True.

My attempts thus far fail to check within each row, and just look to see if the first column string is present in ALL rows column 2.

Here is an example:

import pandas as pd

df = pd.DataFrame({'a':['a','b','c','d','e'],
                   'b':['a/b','c/d','c/a','a/b','e']})

df['a'].isin(df['b'])

Expected result would evaluate to:

True
False
True
False
True

Comprehension

[a in b for a, b in zip(df.a, df.b)]

[True, False, True, False, True]

df.assign(In=[a in b for a, b in zip(df.a, df.b)])

   a    b     In
0  a  a/b   True
1  b  c/d  False
2  c  c/a   True
3  d  a/b  False
4  e    e   True

Numpy

from numpy.core.defchararray import find

a, b = df.values.astype(str).T
find(b, a) >= 0

array([ True, False,  True, False,  True])

df.assign(In=find(b, a) >= 0)

   a    b     In
0  a  a/b   True
1  b  c/d  False
2  c  c/a   True
3  d  a/b  False
4  e    e   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