您好我有以下数据框:
> df1
col1
0 donald
1 mike
2 donald trump
3 trump
4 mike pence
5 pence
6 jarred
我想检查包含此列中子字符串的字符串,并创建一个新列,如果条件已满,则保存较大的字符串
类似的东西:
> df1
col1 col2
0 donald donald trump
1 mike mike pence
2 donald trump donald trump
3 trump donald trump
4 mike pence mike pence
5 pence mike pence
6 jarred jarred
提前致谢
答案 0 :(得分:3)
这应该这样做:
df['Col2'] = df['Col1'].apply(lambda x: max([i for i in df['Col1'] if x in i], key=len))