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.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • Do any of the links here help? https://stackoverflow.com/questions/64182958/adding-a-column-in-pandas-with-a-function?noredirect=1&lq=1 – Arthur Morris Oct 21 '20 at 06: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 – Carl Clifford Oct 27 '20 at 03:11

1 Answers1

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

Arthur Morris
  • 1,253
  • 1
  • 15
  • 21