I have a pandas dataframe that looks like this:
Index | Year | Month | Fruit | Count |
---|---|---|---|---|
0 | 2021 | 03 | Apple | 2 |
1 | 2021 | 03 | Orange | 3 |
2 | 2021 | 03 | Mango | 4 |
3 | 2021 | 04 | Apple | 1 |
4 | 2021 | 04 | Mango | 2 |
5 | 2021 | 05 | Apple | 1 |
Let's say that Apple, Orange and Mango are the only fruits I am interested in.
The dataframe is formed such that if the count for a fruit (in a month and year) is 0, it won't show up as a record. I want to add the missing fruit row to the dataframe with count column value = 0. In short, I want each of the 3 fruits to show up for the year and month irrespective of the count being 0. So I want it to look like this:
Index | Year | Month | Fruit | Count |
---|---|---|---|---|
0 | 2021 | 03 | Apple | 2 |
1 | 2021 | 03 | Orange | 3 |
2 | 2021 | 03 | Mango | 4 |
3 | 2021 | 04 | Apple | 1 |
x | 2021 | 04 | Orange | 0 |
4 | 2021 | 04 | Mango | 2 |
5 | 2021 | 05 | Apple | 1 |
x | 2021 | 05 | Orange | 0 |
x | 2021 | 05 | Mango | 0 |
Appreciate any suggestions on how to approach this.