-1

I have a dataframe that looks like

import pandas as pd
   df = pd.DataFrame({'Subject1': ['Math', 'BioScience', 'PhysicalScience',
                                'Sociology', 'Psychology', 'Arts'],
                       'Subject2': ['BioScience', 'PhysicalScience', 'Sociology', 
                                   'Arts', 'Arts', 'PhysicalScience'],
                       'points': [10, 8, 10, 6, 6, 5]})

I would like to delete the rows where Subject1 or Subject2 column contains the string "Science".

DPdl
  • 723
  • 7
  • 23
  • Does this answer your question? [Filter pandas DataFrame by substring criteria](https://stackoverflow.com/questions/11350770/filter-pandas-dataframe-by-substring-criteria) – ddejohn Nov 30 '22 at 00:38
  • Does this answer your question? [Filtering pandas dataframe rows by contains str](https://stackoverflow.com/questions/32616261/filtering-pandas-dataframe-rows-by-contains-str) – SomeDude Nov 30 '22 at 00:39

1 Answers1

1

here is one way to do it


# choose rows where subject1 or subject2 contains 'science'
# negate the result to choose rows that fails the match

out=df.loc[~(df['Subject1'].str.contains('Science') | 
             df['Subject2'].str.contains('Science'))]
out
Subject1    Subject2    points
3   Sociology   Arts    6
4   Psychology  Arts    6
Naveed
  • 11,495
  • 2
  • 14
  • 21