I have a dataframe sorted by amount giving me top 5 categories per Name like this:
| Name | Category | Amount |
|------|----------|--------|
| Abel | A | 9.2 |
| Abel | B | 3 |
| Abel | C | 2.5 |
| Abel | E | 2 |
| Abel | X | 0 |
| Cain | W | 93 |
| Cain | A | 2 |
|------|----------|--------|
This is what I want in the end:
| Name | Cat 1 | Cat 2 | Cat 3 | Cat 4 | Cat 5 |
|------|-------|-------|-------|-------|-------|
| Abel | A | B | C | E | X |
| Cain | W | A | - | - | - |
|------|-------|-------|-------|-------|-------|
I tried df.pivot("Name","Category") but it's setting the values (e.g. A, B, ...) as the column names but I want the 5 columns to be predefined as "Cat 1" to "Cat 5" instead so I'm not sure what can I do to get the result now. Also, not all names have 5 rows. For e.g. Cain has only top 2, which mean Cat 3, Cat 4 and Cat5 columns should be null or "-". Any help? Thanks!
Updates:
Ok, so for e.g. if all my names have only 2 categories record, I want to still get 5 new columns for top 5 categories (i.e. Cat 1, Cat 2, Cat 3, Cat 4, Cat 5).
Now if I do
df["g"] = top5_jmi.groupby("Name").cumcount().add(1)
This will only give me 2 columns if I pivot it later. How can I get 5 columns? E.g.
| Name | Category | Amount |
|------|----------|--------|
| Abel | A | 9.2 |
| Abel | B | 3 |
| Cain | W | 93 |
| Cain | A | 2 |
|------|----------|--------|
should still give me this:
| Name | Cat 1 | Cat 2 | Cat 3 | Cat 4 | Cat 5 |
|------|-------|-------|-------|-------|-------|
| Abel | A | B | - | - | - |
| Cain | W | A | - | - | - |
|------|-------|-------|-------|-------|-------|