Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何查找数据帧中包含子字符串的所有行?_Python_String_Pandas_Dataframe_String Matching - Fatal编程技术网

Python 如何查找数据帧中包含子字符串的所有行?

Python 如何查找数据帧中包含子字符串的所有行?,python,string,pandas,dataframe,string-matching,Python,String,Pandas,Dataframe,String Matching,我有一个单词和一个数据框,其中有一列字符串值。现在,我试图在数据框中找到在字符串部分包含该单词的行 我读过extractall()方法,但我不确定如何使用它,或者它是否是正确的答案。使用 演示 使用该测试数据(修改和借用): 您可以使用它来查找仅包含单词goons的行(我忽略大小写): 以jato为例 In [148]: df[['Goons' in i for i in df.regiment]] Out[148]: regiment company na

我有一个单词和一个数据框,其中有一列字符串值。现在,我试图在数据框中找到在字符串部分包含该单词的行

我读过
extractall()
方法,但我不确定如何使用它,或者它是否是正确的答案。

使用


演示

使用该测试数据(修改和借用):

您可以使用它来查找仅包含单词
goons
的行(我忽略大小写):

以jato为例

In [148]: df[['Goons' in i for i  in  df.regiment]]
Out[148]:
           regiment company      name  preTestScore  postTestScore
0  Nighthawks Goons     1st    Miller             4             25
1  Nighthawks Goons     1st  Jacobson            24             94

它是否与此处提到的问题类似,您可以使用
df[df['column\u name'].str.contains(“您的字符串”)]
myword = 'foo'
df = pd.DataFrame(dict(mycolumn=['abc', '__foo__']))

df.mycolumn.str.contains(myword)

0    False
1     True
Name: mycolumn, dtype: bool
raw_data = {'regiment': ['Nighthawks Goons', 'Nighthawks Goons', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'],
        'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'],
        'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'],
        'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
        'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore'])
df[df['regiment'].str.contains(r"\bgoons\b", case = False)]
In [148]: df[['Goons' in i for i  in  df.regiment]]
Out[148]:
           regiment company      name  preTestScore  postTestScore
0  Nighthawks Goons     1st    Miller             4             25
1  Nighthawks Goons     1st  Jacobson            24             94