Skip to content
Advertisement

Collapsing rows

I have the following table below:

I would like to collapse Code_1 and Code_2 columns based on ID and Date. Based on what I have found online, I have tried the below snippet of code but it does not seem to be working.

df= df.groupby(['ID','Date']).agg(''.join)

DF:

ID Date Count_Code1 Count_Code2 Code_1 Code_2
A1 2022-02-02 90 0 AAAA NaN
A1 2022-02-02 0 50 NaN BBBB
A1 2022-03-14 34 0 AAAA NaN
C1 2022-04-20 0 13 NaN BBBB
C1 2022-04-20 15 0 AAAA NaN

Desired output:

ID Date Count_Code1 Count_Code2 Code_1 Code_2
A1 2022-02-02 90 50 AAAA BBBB
A1 2022-03-14 34 0 AAAA NaN
C1 2022-04-20 15 13 AAAA BBBB

Advertisement

Answer

JavaScript
JavaScript

Alternate Solution

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement