python - Searching Multiple Strings in pandas without predefining number of strings to use -
i'm wondering if there's more general way below? i'm wondering if there's way create st function can search non-predefined number of strings?
so instance, being able create generalized st function, , type st('governor', 'virginia', 'google)
here's current function, predefines 2 words can use. (df pandas dataframe)
def search(word1, word2, word3 df): """ allows search intersection of 3 terms """ return df[df.name.str.contains(word1) & df.name.str.contains(word2) & df.name.str.contains(word3)] st('governor', 'virginia', newauthdf)
you use np.logical_and.reduce:
import pandas pd import numpy np def search(df, *words): #1 """ return sub-dataframe of rows name column match words. """ return df[np.logical_and.reduce([df['name'].str.contains(word) word in words])] # 2 df = pd.dataframe({'name':['virginia google governor', 'governor virginia', 'governor virginia google']}) print(search(df, 'governor', 'virginia', 'google')) prints
name 0 virginia google governor 2 governor virginia google - the
*indef search(df, *words)allowssearchaccept unlimited number of positional arguments. collect arguments (after first) , place them in list calledwords. - np.logical_and.reduce([x,y,z]) equivalent
x & y & z. allows handle arbitrarily long list, however.
Comments
Post a Comment