使用@jezrael 的设置
df = pd.DataFrame(
{'Title':['test','Test','e', 'a'],
'Subtitle':['b','a','Test', 'a']})
pandas
你可以stack + str.contains + unstack
import re
df.stack().str.contains('test', flags=re.IGNORECASE).unstack()
Subtitle Title
0 False True
1 False True
2 True False
3 False False
把所有东西都放在一起
truth_map = {True: 'Y', False: ''}
truth_flag = df.stack().str.contains(
'test', flags=re.IGNORECASE).unstack().any(1).map(truth_map)
df.assign(Test_flag=truth_flag)
Subtitle Title Test_flag
0 b test Y
1 a Test Y
2 Test e Y
3 a a
numpy
如果性能是一个问题
v = df.values.astype(str)
low = np.core.defchararray.lower(v)
flg = np.core.defchararray.find(low, 'test') >= 0
ys = np.where(flg.any(1), 'Y', '')
df.assign(Test_flag=ys)
Subtitle Title Test_flag
0 b test Y
1 a Test Y
2 Test e Y
3 a a
幼稚时间测试