Feb-03-2022, 09:15 PM
You made major changes in your post from original - better to post a reply to your own - you never know if someone is working on it.
Here, I believe this works for you.
Here, I believe this works for you.
import pandas as pd df = pd.DataFrame(['abcdefg','bcde','abcfoo','defgh'], columns=['A']) def fun(the_col): if 'abc' in the_col.A: return 'abc' elif 'de' in the_col.A: return 'de' else: return 'foo' return dfA = df.apply(fun, axis = 1) dfA
Output:0 abc
1 de
2 abc
3 de