Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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 如何检查字符串列是否包含子字符串,如果为True,如何返回行数据?_Python_Pandas_String_Numpy - Fatal编程技术网

Python 如何检查字符串列是否包含子字符串,如果为True,如何返回行数据?

Python 如何检查字符串列是否包含子字符串,如果为True,如何返回行数据?,python,pandas,string,numpy,Python,Pandas,String,Numpy,我有两个独立的数据帧,其中一个带有子字符串,我想检查它们是否包含在包含字符串和行数据的第二个数据帧中。这段代码只会每周运行一次,所以我在尝试使用嵌套for循环进行优化时并不担心优化问题,但似乎无法解决它。例如,我创建了以下命令行,但是子字符串可以位于字符串的开头、中间和结尾-例如: map_df['Number_1'] = [1,2,3,4,5,...,n] map_df['String'] = ['xxhello', 'randomyy', 'zztodayzz',...,n] substri

我有两个独立的数据帧,其中一个带有子字符串,我想检查它们是否包含在包含字符串和行数据的第二个数据帧中。这段代码只会每周运行一次,所以我在尝试使用嵌套for循环进行优化时并不担心优化问题,但似乎无法解决它。例如,我创建了以下命令行,但是子字符串可以位于字符串的开头、中间和结尾-例如:

map_df['Number_1'] = [1,2,3,4,5,...,n]
map_df['String'] = ['xxhello', 'randomyy', 'zztodayzz',...,n]
substring_df['Substring'] = ['hello', 'random', 'today', 'dog', 'cat',..., n]

##Desired result
Substring_df

['Substring']      ['Number_1']
hello                1
random               2
today                3
dog                  
cat
输出:


    map_df_string   substring_df_substring
0   xxhello         hello
1   randomyy        random
2   zztodayzz       today

0    True
1    True
2    True
现在可以执行以下操作

a = df.apply(lambda row: row['substring_df_substring'] in row['map_df_string'], axis=1)
输出:


    map_df_string   substring_df_substring
0   xxhello         hello
1   randomyy        random
2   zztodayzz       today

0    True
1    True
2    True

现在,您可以获取series对象的索引,并在索引为true的位置添加一个加号,以获取
map\u df['Number\u 1']

数据帧列(A
pd.series
)具有带有大量字符串函数的
str
属性。其中一个可能很有用。您希望如何处理多个事件?