Python 如何检查字符串列是否包含子字符串,如果为True,如何返回行数据?
我有两个独立的数据帧,其中一个带有子字符串,我想检查它们是否包含在包含字符串和行数据的第二个数据帧中。这段代码只会每周运行一次,所以我在尝试使用嵌套for循环进行优化时并不担心优化问题,但似乎无法解决它。例如,我创建了以下命令行,但是子字符串可以位于字符串的开头、中间和结尾-例如: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
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']
数据帧列(Apd.series
)具有带有大量字符串函数的str
属性。其中一个可能很有用。您希望如何处理多个事件?