我正在编写需要在字符串中找到子字符串的代码。 如果字符串较短,则效果很好,但对于很长的长字符串列表,则不起作用。我该如何解决?
例如,对于此代码,它可以工作:
import pandas as pd
data1 = {'spike-2': ["yesnoyesno", "chairchairchair"],
'hey spke': ["maybe maybe maybe", "yes no yes"],
'no': ["aaaaa...VALUE...govora","yesno"]}
df1 = pd.DataFrame(data1)
new_data1 = df1.to_string(header=False,
index=False,
index_names=False).split('\n')
#print(new_data1)
for i in new_data1:
if 'VALUE' in i:
print('found!')
但是对于此代码,它不起作用:
data2 = {'spike-2': ["yesno yesno yesno yes no yesnoyesno yesnoyesno yesnoyesno", "chairchairchair chairchairchair chairchairchair chairchairchair "],
'hey spke': ["maybe maybe maybe", "yes nyes no ye...VALUE...syes no yesyes no yesyes no yesyes no yeso yes"],
'no': ["yes no yesyes no yes yesyes yesyes yes no yes yes no yesaaaaa...VALUE...govora","yesno"]}
df2 = pd.DataFrame(data2)
new_data2 = df2.to_string(header=False,
index=False,
index_names=False).split('\n')
print(new_data2)
for i in new_data2:
if 'VALUE' in i:
print('found!')
答案 0 :(得分:1)
针对您的data2
:
df2 = pd.DataFrame(data2)
result = df2.apply(lambda x: x.str.contains('VALUE'))
给予result
:
spike-2 hey spke no
0 False False True
1 False False False
您可以按行查看结果:
result.any(axis=1)
0 True
1 False
dtype: bool
或按列:
result.any()
spike-2 False
hey spke False
no True
dtype: bool
是您想要的吗?