Created
June 8, 2015 07:16
-
-
Save swayson/454da05a03b60dc616ec to your computer and use it in GitHub Desktop.
Basic snippet for searching items
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def search_item(dataframe, name, query, na=False, case=False, regex=True): | |
idx = pd.Series([False]*len(dataframe)) | |
# For each item in the query look for the item and collect the documents ids it pertains to | |
for q in query: | |
matches = dataframe[text_column].str.contains(q, na=False, case=False, regex=True) | |
idx = (idx) | (matches) | |
dataframe.loc[idx, name] = 1 | |
dataframe.loc[:, name] = dataframe.loc[:, name].fillna(0) | |
return dataframe | |
def make_dataframe(): | |
df = pd.DataFrame(dict(a=['a', 'b', 'c'], b=['A', 'B', 'C']) | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment