简体   繁体   中英

check if string contains sub string from the same column in pandas dataframe

Hi I have the following dataframe:

> df1
  col1  
0 donald     
1 mike
2 donald trump
3 trump
4 mike pence
5 pence
6 jarred

i want to check for the strings that contain sub string from this column and create a new column that holds the bigger strings if the condition is full filled

something like this:

> 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

Thanks in advance

这应该这样做:

df['Col2'] = df['Col1'].apply(lambda x: max([i for i in df['Col1'] if x in i], key=len))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM