How to combine AND and OR operator in pandas data frame?

My goal is to find out if there are certain combinations of keywords that may be present in a column filled with text lines (headings of news articles). Then I want to plot the frequency in the histogram.

I did the following using the pandas data frame:

pvv_news = df[df['desc'].str.contains("pvv", case=True)]
pvv_month = win.groupby(win.index.month).size()
pvv_month.index = ['January', 'February', 'March', 'April', 'May', 'June']
pvv_month.plot(kind='bar')

What gives:

enter image description here

Now I can’t understand how to make AND and OR combinations to get more specific results. An example of what I mean, but that does not work:

pvv_news = df[df['desc'].str.contains("(pvv)&(nederland|overheid)", case=True)]

I looked at the following functions, but I can not understand:

  • pandas.Series.str.extract
  • pandas.Series.str.match
  • pandas.Series.str.contains
  • Regular expressions in combination with the above functions.
+4
source share
1

, , :

pvv_news = df[(df['desc'].str.contains("pvv"), case = True) &
              ((df['desc'].str.contains("nederland"), case = True) |  
               (df['desc'].str.contains("overheid"), case = True)) ]
+4

All Articles