Skip to content

Instantly share code, notes, and snippets.

@sevperez
Last active October 14, 2020 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sevperez/3c6233f846d98fd5e3e11e5c40c076f4 to your computer and use it in GitHub Desktop.
Save sevperez/3c6233f846d98fd5e3e11e5c40c076f4 to your computer and use it in GitHub Desktop.
def search_df_texts(df, query_string: str):
"""
- Parameters: df (Pandas DataFrame), query_string (string). df must
contain a "text" column.
- Returns: A subset of df containing only rows where each term in
query_string appeared as a substring in df["text"].
"""
terms = query_string.lower().split(" ")
filters = [df["text"].str.lower().str.contains(term) for term in terms]
return df[np.all(filters, axis=0)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment