reshape pandas DF to show all id

Viewed 14

I have the following pandas dataframe:

+------------+----+--------+
|    date    | id | number |
+------------+----+--------+
| 2022-05-02 | A  | 397177 |
| 2022-04-29 | A  |  53876 |
| 2022-04-29 | B  | 191214 |
| 2022-04-28 | A  |  75824 |
| 2022-04-28 | B  | 483860 |
| 2022-04-28 | C  |  51731 |
+------------+----+--------+

It is the result of a query on a database.
There are only 3 different ID possible (A, B or C), but it could very well be that there are no data for some ID on some day (e.g., 2nd of May there were no records found for B and C, on the 29th of April there were no records found for C).

In the end, I would like to reshape the dataframe in order to have all IDs for each day, even if the number is 0.

Expected output:

+------------+----+--------+
|    date    | id | number |
+------------+----+--------+
| 2022-05-02 | A  | 397177 |
| 2022-05-02 | B  |      0 |
| 2022-05-02 | C  |      0 |
| 2022-04-29 | A  |  53876 |
| 2022-04-29 | B  | 191214 |
| 2022-04-29 | C  |      0 |
| 2022-04-28 | A  |  75824 |
| 2022-04-28 | B  | 483860 |
| 2022-04-28 | C  |  51731 |
+------------+----+--------+

Is that possible in Python using pandas?

0 Answers
Related