Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. def str_search(*substrings: str, iterable, exact_match: bool = False) -> list:
  2. """Case-insensitive search of an iterable for substrings.
  3.  
  4. Args:
  5. substrings (str): strings to search for in the iterable.
  6. iterable (list, tuple, etc): iterable containing string objects to be
  7. searched.
  8. exact_match (bool): if True only return a single value that exactly
  9. matches the substring supplied (therefore only works if 1 substring
  10. arg is supplied). Otherwise returns list of all partial matches.
  11. """
  12. lower_to_real = {str.lower(i): i for i in iterable if isinstance(i, str)}
  13. strings = [str.lower(s) for s in substrings if isinstance(s, str)]
  14. if exact_match:
  15. try:
  16. match = [v for k, v in lower_to_real.items() if all([s == k for s in strings])][0]
  17. except IndexError:
  18. return list()
  19. else:
  20. match = [v for k, v in lower_to_real.items() if all([s in k for s in strings])]
  21. return match
  22.  
  23.  
  24. def str_search_df(*keywords, df):
  25. """Search all columns of a Pandas DataFrame for strings matching keywords.
  26. """
  27. mask = (~df.index.isin(list(df.index)))
  28. for col in df.columns:
  29. matches = str_search(*keywords, iterable=df[col].astype(str))
  30. if len(matches):
  31. mask = mask | (df[col].isin(matches))
  32. return df[mask]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement