Skip to content

Instantly share code, notes, and snippets.

@KalebNyquist
Last active February 9, 2023 14:34
Show Gist options
  • Save KalebNyquist/6781634b4ad307576046352696d2d194 to your computer and use it in GitHub Desktop.
Save KalebNyquist/6781634b4ad307576046352696d2d194 to your computer and use it in GitHub Desktop.
Find Index Location Values in Pandas Dataframe for Specific String Value
def find_locations_in_pandas_dataframe(dataframe, search_term, starts_with_search_term=False):
""" Returns an ordered list of (dataframe column, dataframe row, specific string) index values where `search_term` is
found in Pandas `dataframe` string data.
Keyword arguments:
starts_with_search_term: if set to True, then returns only strings that start with `search_term`
i.e., "Re" in "Rebecca" but not "González, Rebecca"
Source: https://gist.github.com/KalebNyquist/6781634b4ad307576046352696d2d194
"""
locations = []
dataframe_width = len(dataframe.columns)
for column_index in range(dataframe_width):
occurences = dataframe[dataframe.columns[column_index]].str.find(search_term)
row_index = 0
for possible_occurence in occurences:
if starts_with_search_term == False and possible_occurence >= 0:
locations.append((int(column_index), int(row_index), int(possible_occurence)))
if starts_with_search_term == True and possible_occurence == 0:
locations.append((int(column_index), int(row_index)))
row_index += 1
return locations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment