0

I am trying to combine multiple columns and rows into single column based on group by field of ID column. The input is

|Id |   Sample_id | Sample_name |   Sample_number|
|:--|:------------|:-----------:|---------------:|             
|1  |  123        | Abcdef|ghij |  1234567       |
|1  |   345       | Vbnhj|tt|t  |   45678        |
|1  |   456       | Ffff|yyy|yy |   789000       |

Expected output : columns,rows belonging to same Id should be combined and form a list like below.

Id  Sample_details              
    123,Abcdef|ghij,1234567
 1  345,Vbnhj|tt|t, 45678       
    456 ,Ffff|yyy|yy,789000

 2   536 ,Ftff|uyy|iy,79000
     453, hnhj|tdd|rr, 67678 
   

I tried below which is not working

df.groupby('Id')['Sample_id']['Sample_name']['Sample_number'].apply(','.join).reset_index()
2
  • Your outcome is not clear. Do you want to join by ',' for each row and join rows by '\n' to keep only one Id? So what do you mean by combined and form a list like? Commented Sep 23, 2021 at 12:42
  • yes you are correct , i want to join each column by , and each row by \n , for the grouped id. Commented Sep 23, 2021 at 12:55

1 Answer 1

1

Try:

df['Sample_details'] = df.filter(like='Sample_').astype(str).apply(','.join, axis=1)

out = df.groupby('Id')['Sample_details'].apply('\n'.join).reset_index()

Output:

>>> out
   Id                                     Sample_details
0   1  123,Abcdef|ghij,1234567\n345,Vbnhj|tt|t,45678\...

Note: Pandas does not interpret escape sequence.

Sign up to request clarification or add additional context in comments.

3 Comments

sorry, im not expecting this. all the list should be in single column row, i dont want 3 different rows in sample details. i'm updating the format in my question how my output should be represented. Thanks
I have updated question with the expected output format
@behappy. I updated my answer too if you want to check.

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.