0

Here's the thing, I have something like:

col1                          col2
This is a blue book           blue book above
This is a green ball          this is a ball
What is your name             blue book above

I want to create col3 like this:

col1                          col2                  col3
this is a blue book           blue book above       this is a blue book above
this is a green ball          this is a ball        this is a green ball
what is your name             blue book above       what is your name blue book above

I can't find a way to make this work

7
  • Does it matter what order the words are in, or is the condition just that if a word is in both col1 and col2 it should only be included in col3 once? Commented Aug 19, 2020 at 21:41
  • 1
    Please repeat on topic and how to ask from the intro tour. “Show me how to implement this feature” is not a Stack Overflow issue. You have to make an honest attempt, and then ask a specific question about your algorithm or technique. Commented Aug 19, 2020 at 21:42
  • This consists of several steps: concatenate two strings, break into words, de-dup the word lists, rejoin into a string, and create a new column with that value. Where are you stuck? What parts do you have working? Post your code! Commented Aug 19, 2020 at 21:44
  • @Prune ok, sorry about that Commented Aug 19, 2020 at 21:44
  • @ChrisDoyle not so much, I would rather have in the order I mentioned, but that wouldn't break anything Commented Aug 19, 2020 at 21:45

1 Answer 1

2

concatenate two string columns in Pandas, but excluding repeated words from the second one

Try this:

def f(r):
  c1,c2 = r
  s1 = c1.split(" ")
  s2 = c2.split(" ")
  s3 = [s for s in s2 if s not in s1]
  return c1+" "+" ".join(s3)
df["col3"] = df[["col1", "col2"]].apply(f, axis=1)
df
                   col1               col2                               col3
0   this is a blue book    blue book above          this is a blue book above
1   this is a green ball    this is a ball               this is a green ball
2   what is your name      blue book above  what is your name blue book above
Sign up to request clarification or add additional context in comments.

Comments

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.