'How to partially pivot a table in pandas
I'm not sure if "pivot" is the right term for this, but I have something like
| ID | type | date |
|---|---|---|
| 123 | A | 1-2-2020 |
| 123 | B | 1-7-2020 |
| 123 | C | 1-14-2020 |
| 456 | A | 5-18-2019 |
| 456 | B | 5-30-2019 |
| 789 | A | 8-7-2021 |
| 789 | B | 8-16-2021 |
| 789 | C | 8-23-2021 |
| 789 | D | 8-30-2021 |
And I want to transform it into something like:
| ID | type_1 | date_1 | type_2 | date_2 |
|---|---|---|---|---|
| 123 | A | 1-2-2020 | B | 1-7-2020 |
| 123 | B | 1-7-2020 | C | 1-14-2020 |
| 456 | A | 5-18-2019 | B | 5-30-2019 |
| 789 | A | 8-7-2021 | B | 8-16-2021 |
| 789 | B | 8-16-2021 | C | 8-23-2021 |
| 789 | C | 8-23-2021 | D | 8-30-2021 |
So that they types are paired and every row has exactly two types and dates.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
