Claudiu Creanga
Claudiu Creanga

Reputation: 8366

Is there a way in pandas to filter rows in a column that are contained in a string

There is a way to check if the string in the column contains another string:

df["column"].str.contains("mystring")

But I'm looking for the opposite, the column string to be contained in another string, without doing an apply function which I guess is slower than the vectorised .contains:

df["column"].apply(lambda x: x in "mystring", axis=1)

Update with data:

mystring = "abc"
df = pd.DataFrame({"A": ["ab", "az"]})
df
    A
0   ab
1   az

I would like to show only "ab" because it is contained in mystring.

Upvotes: 3

Views: 45

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

pd.Series([i in mystring for i in df.A]) 

Output:

0     True
1    False
dtype: bool

Upvotes: 2

cs95
cs95

Reputation: 402523

Only one option (jpp had it) - iterate with a list comprehension:

df[[r in mystring for r in df.A]]

    A
0  ab

Or,

pd.DataFrame([r for r in df.A if r in mystring], columns=['A'])

    A
0  ab

Upvotes: 2

Related Questions