Problem using substring to select data frame rows

Python 2.7

In [3]:import pandas as pd
df = pd.DataFrame(dict(A=['abc','abc','abc','xyz','xyz'],
                       B='abcdef','abcdefghi','notthisone','uvwxyz','orthisone']))
In [4]: df
Out[4]:
    A   B
0   abc abcdef
1   abc abcdefghi
2   abc notthisone
3   xyz uvwxyz
4   xyz orthisone

In [12]:  df[df.B.str.contains(df.A) == True] 
# just keep the B that contain A string

TypeError: 'Series' objects are mutable, thus they cannot be hashed

I am trying to do this:

    A   B
0   abc abcdef
1   abc abcdefghi
3   xyz uvwxyz

I tried the str.contains expression options but did not go. Any help is greatly appreciated.

+4
source share
4 answers

It doesn't seem to str.containssupport multiple patterns, so you just need to apply them line by line:

substr_matches = df.apply(lambda row: row['B'].find(row['A']) > -1, axis=1)

df.loc[substr_matches]
Out[11]: 
     A          B
0  abc     abcdef
1  abc  abcdefghi
3  xyz     uvwxyz
+2
source

Apply lambda function to strings and check if A is in B.

>>> df[df.apply(lambda x: x.A in x.B, axis=1)]
     A          B
0  abc     abcdef
1  abc  abcdefghi
3  xyz     uvwxyz
+2
source

You can call uniquein column "A" and then join in |to create a template for matching using contains:

In [15]:
df[df['B'].str.contains('|'.join(df['A'].unique()))]

Out[15]:
     A          B
0  abc     abcdef
1  abc  abcdefghi
3  xyz     uvwxyz
+1
source

How about this?

In [8]: df[df.apply(lambda v: v['A'] in v['B'], axis=1)]
Out[8]: 
     A          B
0  abc     abcdef
1  abc  abcdefghi
3  xyz     uvwxyz
+1
source

All Articles