在 pandas 中搜索多个字符串而无需预定义要使用的字符串数 [英] Searching Multiple Strings in pandas without predefining number of strings to use
本文介绍了在 pandas 中搜索多个字符串而无需预定义要使用的字符串数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道下面是否有更通用的方法?我想知道是否有一种方法可以创建st函数,以便我可以搜索未预定义数量的字符串?
I'm wondering if there's a more general way to do the below? I'm wondering if there's a way to create the st function so that I can search a non-predefined number of strings?
例如,能够创建通用的st函数,然后键入st('Governor','Virginia','Google)
So for instance, being able to create a generalized st function, and then type st('Governor', 'Virginia', 'Google)
这是我当前的功能,但它预定义了您可以使用的两个词. (df是熊猫的DataFrame)
here's my current function, but it predefines two words you can use. (df is a pandas DataFrame)
def search(word1, word2, word3 df):
"""
allows you to search an intersection of three terms
"""
return df[df.Name.str.contains(word1) & df.Name.str.contains(word2) & df.Name.str.contains(word3)]
st('Governor', 'Virginia', newauthdf)
推荐答案
您可以使用np.logical_and.reduce
:
import pandas as pd
import numpy as np
def search(df, *words): #1
"""
Return a sub-DataFrame of those rows whose Name column match all the words.
"""
return df[np.logical_and.reduce([df['Name'].str.contains(word) for word in words])] # 2
df = pd.DataFrame({'Name':['Virginia Google Governor',
'Governor Virginia',
'Governor Virginia Google']})
print(search(df, 'Governor', 'Virginia', 'Google'))
打印
Name
0 Virginia Google Governor
2 Governor Virginia Google
-
def search(df, *words)
中的*
允许search
接受 无限数量的位置参数.它将收集所有 参数(在第一个参数之后)并将其放在名为words
的列表中. - np.logical_and.reduce([X,Y ,Z])等同于
X & Y & Z
.它 但是,您可以处理任意长的列表.
- The
*
indef search(df, *words)
allowssearch
to accept an unlimited number of positional arguments. It will collect all the arguments (after the first) and place them in a list calledwords
. - np.logical_and.reduce([X,Y,Z]) is equivalent to
X & Y & Z
. It allows you to handle an arbitrarily long list, however.
这篇关于在 pandas 中搜索多个字符串而无需预定义要使用的字符串数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文