1

Context

I have a Pandas DataFrame and would like to filter it by only including Rows that contain any String from a given List. However, I can't find a solution in the Pandas Documentation.


Code

DataFrame

ID      Names
0       'NameA NameB'
1       'NameB'
2       'NameB NameC'
3       'NameC'
data: pandas.DataFrame = ...
names = ['NameA', 'NameC']

filteredData = data.filter ... # ?

In this example, when filtering, only the Row with ID = 1 should be removed, since it does not contain one the defined Names.


Question

  • How can I achieve the described goal above?
0

1 Answer 1

3

use:

df2=data.loc[data['Names'].str.contains('|'.join(names))]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for your answer. However, how can .join do the trick when the output looks like this: NameA|NameC, which is a String, none of the Rows actually contains?
"|" operator means or. like this: NameA or NameC...
I know, however, I didn't know that .contains supports this inside a String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.