Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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 findall和regex_Python_Regex_Search_Find - Fatal编程技术网

在整行搜索时使用python findall和regex

在整行搜索时使用python findall和regex,python,regex,search,find,Python,Regex,Search,Find,下面是在整行中搜索给定单词的代码。代码来自我之前的代码。目前,python搜索行中给定单词的出现情况。但我只想找到完整的单词 当python搜索'jo'时,它不应该返回任何结果,因为没有单词'jo',但是当搜索'jones'时,python应该在第一行返回5 1我应该如何修改我的搜索?我知道我必须使用正则表达式。但我不知道如何实施。 我尝试了findall?I\b搜索字符串\b,但出现错误 如果任何列具有数据类型float,下面的代码将给出错误。为了克服这个问题,我将原始数据框拆分为非数字列和数

下面是在整行中搜索给定单词的代码。代码来自我之前的代码。目前,python搜索行中给定单词的出现情况。但我只想找到完整的单词

当python搜索'jo'时,它不应该返回任何结果,因为没有单词'jo',但是当搜索'jones'时,python应该在第一行返回5

1我应该如何修改我的搜索?我知道我必须使用正则表达式。但我不知道如何实施。 我尝试了findall?I\b搜索字符串\b,但出现错误

如果任何列具有数据类型float,下面的代码将给出错误。为了克服这个问题,我将原始数据框拆分为非数字列和数字列,在代码下面运行,然后将数字列合并回来。有没有一种优雅的方法可以做到这一点

sales = [{'account': 'jones', 'Jan': '150 jones', 'Feb': '200 jones', 'Mar': '140 jones jones'},
         {'account': '1',  'Jan': 'Jones', 'Feb': '210', 'Mar': '215'},
         {'account': '1',  'Jan': '50',  'Feb': '90',  'Mar': '95' }]
df = pd.DataFrame(sales)
df

df_list = []

search_string='jones'
for search_string in ['jo', 'jones']:
    #use above method but rename the series instead of setting to
    # a columns. The append to a list.
    df_list.append(df.apply(lambda x: x.str.lower().str.findall(search_string).str.len()).sum(axis=1).astype(int).rename(search_string))

#concatenate the list of series into a DataFrame with the original df
df = pd.concat([df] + df_list, axis=1)
df
使用下面给出的答案的更新代码

sales = [{'account': 'jones.', 'Jan': '150 jones', 'Feb': '200 .jones', 'Mar': '140 jones jones'},
         {'account': '1',  'Jan': 'Jones', 'Feb': '210', 'Mar': '215'},
         {'account': '1',  'Jan': '50',  'Feb': '90',  'Mar': '95' }]
df = pd.DataFrame(sales)
df

df_list = []

search_string='jones'
for search_string in ['jones.', 'jone','jones']:
    #use above method but rename the series instead of setting to
    # a columns. The append to a list.
    df_list.append(df.apply(lambda x: x.str.lower().str.findall(r'\b{0}\b'.format(search_string)).str.len()).sum(axis=1).astype(int).rename(search_string))

#concatenate the list of series into a DataFrame with the original df
df = pd.concat([df] + df_list, axis=1)
df

如果试图将搜索字符串放入正则表达式中,然后进行匹配,则应执行以下操作:

import re
test_str = ("account"
                "jones"
                "Jan"
                "150 jones"
                "Feb"
                "200 jones"
                "Mar"
                "140 jones jones")

for search_string in ['jo', 'jones']:
    regex = r'\b{0}\b'.format(search_string)
    number_of_matches = len(re.findall(regex, test_str))

    print(number_of_matches)
还没有能够测试熊猫,但应该给你足够的工作