shbfy
shbfy

Reputation: 2135

String contains across two pandas series

I have a series with some strings in a pandas dataframe. I would like to search for the existence of that string within an adjacent column.

In the below example I would like to search for if the string in 'choice' series is contained within the 'fruit' series, returning either true (1) or false (0) in a new column 'choice_match'.

Example DataFrame:

import pandas as pd
d = {'ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'fruit': [
'apple, banana', 'apple', 'apple', 'pineapple', 'apple, pineapple',            'orange', 'apple, orange', 'orange', 'banana', 'apple, peach'],
'choice': ['orange', 'orange', 'apple', 'pineapple', 'apple', 'orange',  'orange', 'orange', 'banana', 'banana']}
df = pd.DataFrame(data=d)

Desired DataFrame:

import pandas as pd
d = {'ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'fruit': [
'apple, banana', 'apple', 'apple', 'pineapple', 'apple, pineapple',   'orange', 'apple, orange', 'orange', 'banana', 'apple, peach'],
'choice': ['orange', 'orange', 'apple', 'pineapple', 'apple', 'orange',      'orange', 'orange', 'banana', 'banana'],
'choice_match': [0, 0, 1, 1, 1, 1, 1, 1, 1, 0]}
df = pd.DataFrame(data=d)

Upvotes: 4

Views: 504

Answers (4)

piRSquared
piRSquared

Reputation: 294258

Option 1
Use Numpy's find
When find doesn't find the value, it returns -1

from numpy.core.defchararray import find

choice = df.choice.values.astype(str)
fruit = df.fruit.values.astype(str)

df.assign(choice_match=(find(fruit, choice) > -1).astype(np.uint))

   ID     choice             fruit  choice_match
0   1     orange     apple, banana             0
1   2     orange             apple             0
2   3      apple             apple             1
3   4  pineapple         pineapple             1
4   5      apple  apple, pineapple             1
5   6     orange            orange             1
6   7     orange     apple, orange             1
7   8     orange            orange             1
8   9     banana            banana             1
9  10     banana      apple, peach             0

Option 2
Set logic
With sets < is strict subset and <= is subset. Make yourself some pd.Series of sets and use <= to find out if one column's sets are subsets of the other column's sets.

choice = df.choice.apply(lambda x: set([x]))
fruit = df.fruit.str.split(', ').apply(set)

df.assign(choice_match=(choice <= fruit).astype(np.uint))

   ID     choice             fruit  choice_match
0   1     orange     apple, banana             0
1   2     orange             apple             0
2   3      apple             apple             1
3   4  pineapple         pineapple             1
4   5      apple  apple, pineapple             1
5   6     orange            orange             1
6   7     orange     apple, orange             1
7   8     orange            orange             1
8   9     banana            banana             1
9  10     banana      apple, peach             0

Option 3
Inspired by @Wen's answer
Using get_dummies and max

c = pd.get_dummies(df.choice)
f = df.fruit.str.get_dummies(', ')
df.assign(choice_match=pd.DataFrame.mul(*c.align(f, 'inner')).max(1))

   ID     choice             fruit  choice_match
0   1     orange     apple, banana             0
1   2     orange             apple             0
2   3      apple             apple             1
3   4  pineapple         pineapple             1
4   5      apple  apple, pineapple             1
5   6     orange            orange             1
6   7     orange     apple, orange             1
7   8     orange            orange             1
8   9     banana            banana             1
9  10     banana      apple, peach             0

Upvotes: 4

BENY
BENY

Reputation: 323236

Ummm find a interesting way get_dummies

(df.fruit.str.replace(' ','').str.get_dummies(',')+df.choice.str.get_dummies()).gt(1).any(1)
Out[726]: 
0    False
1    False
2     True
3     True
4     True
5     True
6     True
7     True
8     True
9    False
dtype: bool

After assign it back

df['New']=(df.fruit.str.replace(' ','').str.get_dummies(',')+df.choice.str.get_dummies()).gt(1).any(1).astype(int)
df
Out[728]: 
   ID     choice             fruit  New
0   1     orange     apple, banana    0
1   2     orange             apple    0
2   3      apple             apple    1
3   4  pineapple         pineapple    1
4   5      apple  apple, pineapple    1
5   6     orange            orange    1
6   7     orange     apple, orange    1
7   8     orange            orange    1
8   9     banana            banana    1
9  10     banana      apple, peach    0

Upvotes: 3

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

In [75]: df['choice_match'] = (df['fruit']
                                 .str.split(',\s*', expand=True)
                                 .eq(df['choice'], axis=0)
                                 .any(1).astype(np.int8))

In [76]: df
Out[76]:
   ID     choice             fruit  choice_match
0   1     orange     apple, banana             0
1   2     orange             apple             0
2   3      apple             apple             1
3   4  pineapple         pineapple             1
4   5      apple  apple, pineapple             1
5   6     orange            orange             1
6   7     orange     apple, orange             1
7   8     orange            orange             1
8   9     banana            banana             1
9  10     banana      apple, peach             0

Step by step:

In [78]: df['fruit'].str.split(',\s*', expand=True)
Out[78]:
           0          1
0      apple     banana
1      apple       None
2      apple       None
3  pineapple       None
4      apple  pineapple
5     orange       None
6      apple     orange
7     orange       None
8     banana       None
9      apple      peach

In [79]: df['fruit'].str.split(',\s*', expand=True).eq(df['choice'], axis=0)
Out[79]:
       0      1
0  False  False
1  False  False
2   True  False
3   True  False
4   True  False
5   True  False
6  False   True
7   True  False
8   True  False
9  False  False

In [80]: df['fruit'].str.split(',\s*', expand=True).eq(df['choice'], axis=0).any(1)
Out[80]:
0    False
1    False
2     True
3     True
4     True
5     True
6     True
7     True
8     True
9    False
dtype: bool

In [81]: df['fruit'].str.split(',\s*', expand=True).eq(df['choice'], axis=0).any(1).astype(np.int8)
Out[81]:
0    0
1    0
2    1
3    1
4    1
5    1
6    1
7    1
8    1
9    0
dtype: int8

Upvotes: 5

jpp
jpp

Reputation: 164673

Here is one way:

df['choice_match'] = df.apply(lambda row: row['choice'] in row['fruit'].split(','),\
                              axis=1).astype(int)

Explanation

  • df.apply with axis=1 cycles through each row and applies logic; it accepts anonymous lambda functions.
  • row['fruit'].split(',') creates a list from the fruit column. This is necessary so, for example, apple is not considered in pineapple.
  • astype(int) is necessary to convert Boolean values to integers for display purposes.

Upvotes: 5

Related Questions