fiziks
fiziks

Reputation: 257

How do I find all rows such that the string in a column is a substring of a given string in Pandas

In other words, I want to do the reverse of df.str.contains(somestring). I want something like somestring.contained_in(df.column) that still returns a series.

Say I have a string abjk and I have a dataframe

index    letter
0        a
1        b
2        c
...      ...
25       z

Then it should return rows 0, 1, 9, 10 as True. This feels like something that should exist, but I can't seem to find it. Apologies in advance if this is a duplicate.

Upvotes: 0

Views: 36

Answers (1)

Ayoub ZAROU
Ayoub ZAROU

Reputation: 2417

You might just do :

[l in 'abjk' for l in df.letter]

If you want later to return the rows, you could just use numpy.where

import numpy as np
 rows,  = np.where([l in 'abjk' for l in df.letter])

Upvotes: 1

Related Questions