1

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

1 Answer 1

3

This should do it:

df['Col2'] = df['Col1'].apply(lambda x: max([i for i in df['Col1'] if x in i], key=len))
Sign up to request clarification or add additional context in comments.

2 Comments

And dont forget add list comprehension alternative :) +1
@jezrael I can only think of filter at the moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.