I have Pandas DataFrame like below (data types of "ID" and "COL1" is "object"):
ID | COL1 | COL2 | COL3
----|------|------|----
123 | ABc | 55 | G4
123 | Abc | 55 | G4
123 | DD | 55 | G4
44 | RoR | 41 | P0
44 | RoR | 41 | P0
55 | XX | 456 | RR
And I need to:
- Create new column "COL1_cum" where will be all values from "COL1" per ID separated by commas
- Drop duplicated IDs
- Create new column "COL1_num" where will be information how many different levels is in "COL1" per "ID"
So as a result I need something like below:
ID | COL1_cum | COL1_num |COL2 | COL3
----|----------|----------|-----|-----
123 | ABc, DD | 2 | 55 | G4
44 | RoR | 1 | 41 | P0
55 | XX | 1 | 456 | RR
Explanation for COL1_num:
- for ID = 123 COL1_num = 2 because for ID = 123 in "COL1" we have 2 different values: "ABc" and "DD"
- for ID = 44 COL1_num = 1 because for ID = 44 in "COL1" we have 1 value: "RoR"
- for ID = 55 COL1_num = 1 because for ID = 5 in "COL1" we have 1 value: "XX"
How can I do that in Python Pandas?