Joining two rows into one row while keeping content

Viewed 30

I'm trying to merge two rows into one row while keeping the content within them. this is my dataset

  model DLC  DLC  DLC
0 part  arm  arm  arm
1 coord x    y    probability

and this is my target

  model     DLC    DLC    DLC
0 position  arm(x) arm(y) arm(prob..)

I've tried .join and that didn't work. is there an elegant way to solve this?

1 Answers

You can try this to join coord row with part row:

df = df.set_index('model')
df.loc['part'] = df.T.apply(lambda x: '{}({})'.format(x.part, x.coord), axis=1)
df = df.drop(index='coord').reset_index()
print(df)

Prints:

  model     DLC   DLC.1             DLC.2
0  part  arm(x)  arm(y)  arm(probability)
Related