0

I am trying to add columns to a dataframe based on another row of the same dataframe. I would like to look up the row with the first column value equal to the value in column 5 and append columns 2, 3, and 4 to the dataframe as below

1  2  3  4  5
a  b  c  d  i
e  f  g  h  i
i  j  k  l  e

1  2  3  4  5  2a 3a 4a
a  b  c  d  i  j  k  l
e  f  g  h  i  j  k  l
i  j  k  l  e  f  g  h

I have tried creating another table to merge df2 = df.loc[(df.1 == df.5) in various combinations but no luck.

2
  • Do any of the links here help? stackoverflow.com/questions/64182958/… Commented Oct 21, 2020 at 6:58
  • 1
    Thanks for the suggestion. That was a good place to start. In the process I realized though that i could just merge it with itself Commented Oct 27, 2020 at 3:11

1 Answer 1

0

Decided to merge the dataframe with itself:

dfleft = df.merge(df,how='left', left_on='5', right_on='1')

just need to rename columns after that

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.