I am trying to find the unique combinations of the rows and introduce a count column as an int. The idea is to find the total number of unique combinations for the data below (ideally in a new dataframe)
| id | cat_1 | cat_2 | cat_3 | cat_4 |
|---|---|---|---|---|
| 001 | Chips | Null | Null | Null |
| 789 | Chips | Avocado | Null | Null |
| 002 | Chips | Pasta | Null | Null |
| 323 | Chips | Pasta | Null | Null |
| 123 | Chips | Pasta | Cheese | Null |
| 456 | Chips | Sauce | Cheese | Null |
| 101 | Pasta | Null | Null | Null |
| 231 | Pasta | Null | Null | Null |
| 321 | Pasta | Bread | Null | Null |
| 212 | Pasta | Bread | Null | Null |
| 632 | Pasta | Cheese | Null | Null |
I'm imagining the data to look something like this:
| id | cat_1 | cat_2 | cat_3 | cat_4 | count |
|---|---|---|---|---|---|
| 0 | Chips | Null | Null | Null | 1 |
| 1 | Chips | Pasta | Null | Null | 2 |
| 2 | Chips | Pasta | Cheese | Null | 1 |
| 4 | Chips | Sauce | Cheese | Null | 1 |
| 5 | Chips | Avocado | Null | Null | 1 |
| 6 | Pasta | Null | Null | Null | 2 |
| 7 | Pasta | Bread | Null | Null | 2 |
| 8 | Pasta | Cheese | Null | Null | 1 |
I thought I could use something like below however my data actually has up to seven cat_7 and wasn't sure if this was the right way
df1.groupby(['cat_1','cat_2', 'cat_3', 'cat_4']).size().reset_index().rename(columns={0:'count'})
How can I get it into the format?