0

I have the following data frame:

ID_number  Name      Date      S_code    S_description 
1          Dani     01/2017      G1       PROCEDURE ON SINGLE VESSEL
1          Dani     01/2017      R56      INSERTION OF THREE VASCULAR STENTS
1          Dani     06/2017      L34      CHOLECYSTECTOMY
2          Alice    03/2015      L12      OTHER CYSTOSCOPY
3          Elle     04/2015      L34      CHOLECYSTECTOMY
3          Elle     04/2015      H6       EXCISION OR DESTRUCTION OF PERITONEAL TISSUE

I want to combine rows if the columns "ID_Number", "Name" and "Date" are identical, and the columns "S_code" and "S_description" will be concatenated so that the Data will look like this:

    ID_number   Name   Date     S_code     S_description 
        1       Dani   01/2017  G1,R56     PROCEDURE ON SINGLE VESSEL,INSERTION OF THREE VASCULAR STENTS
        1       Dani   06/2017    L34      CHOLECYSTECTOMY
        2       Alice  03/2015    L12      OTHER CYSTOSCOPY
        3       Elle   04/2015  L34,H6     CHOLECYSTECTOMY,EXCISION OR DESTRUCTION OF PERITONEAL TISSUE

The "ID_Number" column is sorted.

I'm new with Python and I will appreciate any help with this issue!

1 Answer 1

1

Use pandas groupby on the three columns, and pass pandas string cat as a function through the agg method :

 df.groupby(['ID_number','Name','Date']).agg(lambda x: x.str.cat(sep=','))

                            S_code  S_description
ID_number   Name    Date        
1           Dani    01/2017 G1,R56  PROCEDURE ON SINGLE VESSEL,INSERTION OF THREE ...
                    06/2017 L34     CHOLECYSTECTOMY
2           Alice   03/2015 L12     OTHER CYSTOSCOPY
3           Elle    04/2015 L34,H6  CHOLECYSTECTOMY,EXCISION OR DESTRUCTION OF PER...
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.