0

I have a pd dataframe that has a column that contains values as ['cat, pet','dog, pet','dog','bird', 'bird, pet','tail', 'cat, tail'], and I want to find all places where s contains both of ['cat', 'pet'], and extract the rows that match.

NOTICE that this question is not made for the specific case ['cat', 'pet'], but for a dynamic input that could handle any combination of lists, and the dataframe is more than 10.000 rows long.

My goal is to filter the rows based on values contained in a specific column

I know that if I want to find 'cat' OR 'pet', I just filter like:

 search = ['cat', 'pet']
 df[df['column'].str.contains('|'.join(search))]

But what if I want to match 'cat' AND 'pet' or other lists with different combinations of values??

I tried:

df[df['column'].str.contains('&'.join(search))]
But it is not working for me :/

Also tried:

np.logical_and.reduce([df['column'].str.contains(word) for word in search])
The Dan
  • 1,408
  • 6
  • 16
  • 41
  • 3
    Take a look at https://stackoverflow.com/questions/37011734/pandas-dataframe-str-contains-and-operation – Chris Feb 12 '21 at 00:44
  • 2
    The reason the OR works is because | is a regex term. There is no direct equivalent for AND. See Chris's linked post for how – noah Feb 12 '21 at 00:50
  • 1
    Also https://stackoverflow.com/questions/61390726/matching-keywords-strings-with-a-pandas-dataframe/61390910#61390910 – ALollz Feb 12 '21 at 00:53
  • Thank you @ALollz, I tried np.logical_and.reduce([dfd['column'].str.contains(word) for word in search]) but is still not working, what I am doing wrong – The Dan Feb 12 '21 at 01:04
  • @TheDan is it possible the values in your column are lists, or a list of lists and not strings? – ALollz Feb 12 '21 at 01:09
  • the values appear to be object whr I use df.dtypes But the error that appears isNameError: name 'df' is not defined, and it actually is defined, I can see the df when debugging, it really does exist and I can use it with other funcs, including df[df['column'].str.contains('|'.join(search))] – The Dan Feb 12 '21 at 01:14

0 Answers0