Pandas dataframe select rows where a list-column contains any of a list of strings

NicoH :

I've got a pandas DataFrame that looks like this:

  molecule            species
0        a              [dog]
1        b       [horse, pig]
2        c         [cat, dog]
3        d  [cat, horse, pig]
4        e     [chicken, pig]

and I like to extract a DataFrame containing only thoses rows, that contain any of selection = ['cat', 'dog']. So the result should look like this:

  molecule            species
0        a              [dog]
1        c         [cat, dog]
2        d  [cat, horse, pig]

What would be the simplest way to do this?

For testing:

selection = ['cat', 'dog']
df = pd.DataFrame({'molecule': ['a','b','c','d','e'], 'species' : [['dog'], ['horse','pig'],['cat', 'dog'], ['cat','horse','pig'], ['chicken','pig']]})
YOBEN_S :

IIUC Re-create your df then using isin with any should be faster than apply

df[pd.DataFrame(df.species.tolist()).isin(selection).any(1)]
Out[64]: 
  molecule            species
0        a              [dog]
2        c         [cat, dog]
3        d  [cat, horse, pig]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas dataframe select rows where a list-column contains a specific set of elements

Select ALL rows where Pandas DataFrame Column values in a List

Select rows where the title contains any term from a list

Checking if column in dataframe contains any item from list of strings

Pandas dataframe - Select rows where one column's values contains a string and another column's values starts with specific strings

Removing rows in a pandas DataFrame where the row contains a string present in a list?

postgres select all rows that has any of list strings in a varchar[] column

Pandas dataframe : Iterate over rows an item in column that contains list

pandas dataframe: how to select rows where one column-value is like 'values in a list'

How to print rows if a list of values appear in any column of pandas dataframe

Pandas Drop rows that do not contains a list of strings

Select rows from a DataFrame based on list values in a column in pandas

Function to replicate rows of dataframe if column contains list

Create a new column from two columns of a dataframe where rows of each column contains list in string format

In Sql Alchemy, how to select rows where any column contains a substring?

LabelEncoding in Pandas on a column with list of strings across rows

Find dataframe rows with all of list of strings in any of list of columns

Pandas: How return all rows if column string contains at least a certain number of strings from a list?

How to filter Pandas Dataframe rows which contains any string from a list?

Pandas: Remove all rows where any of the column contains a certain substring

Pandas dataframe - have column with list of strings

Check if String in List of Strings is in Pandas DataFrame Column

Converting a list to pandas dataframe where list contains dictionary

How to select a list of rows by name in Pandas dataframe?

How to filter Pandas DataFrame by checking whether a Column contains any String from a List?

Select rows from dataframe where column values are lists of strings

Search a list of strings in a column where each cell contains multiple values

Filter for rows if any value in a list of substrings is contained in any column in a dataframe

Pandas convert column where every cell is list of strings to list of integers