0

This is my data frame:

    Location    Category    Value
0          1           A    1.130
1          1           B    2.550
2          2           A    1.130
3          2           B    3.982
4          2           C    1.580
5          3           C    5.070

As you can see, all Location (1 / 2 / 3) do not display all Category (A / B / C). I would like to restructure this data frame so that each Location is now associated with each Category, by putting 0 in the new Value cells; this should lead to the following data frame:

    Location    Category    Value
0          1           A    1.130
1          1           B    2.550
2          1           C    0.000
3          2           A    1.130
4          2           B    3.982
5          2           C    1.580
6          3           A    0.000
7          3           B    0.000
8          3           C    5.070

I know some posts seem to deal with this question, for example Pandas Merging 101, but I did not find how to apply the procedures indicated to my particular case...

I also know that I could manually create the data frame I want, but my original data frame is more complicated than the example given, that's why I'm looking for a pythonic coded way to achieve what I want.

Any idea welcome.

Appendix - create the data frame given as example:

import pandas as pd
 
Location = [1, 1, 2, 2, 2, 3]
Category = ['A', 'B', 'A', 'B', 'C', 'C']
Value = [1.13, 2.55, 1.13, 3.982, 1.58, 5.07]
df = pd.DataFrame({'Location':Location, 'Category':Category, 'Value':Value})

df
Andrew
  • 926
  • 2
  • 17
  • 24
  • Use `pd.MultiIndex.from_product` on the unique values to create all combinations, then reindex using Location/Category as temporary index – mozway Apr 05 '22 at 08:33
  • Or use [pyjanitor's `complete` function](https://pyjanitor-devs.github.io/pyjanitor/api/functions/#janitor.functions.complete.complete) – mozway Apr 05 '22 at 08:36
  • OK, `mozway` I found how to do it. Thanks – Andrew Apr 05 '22 at 10:10

0 Answers0