I have this matrix that contains x number of products and feedback rankings of 5 different categories.
This matrix is represented in a Pandas DataFrame like this:
index | Bad ratings | Slightly bad ratings | Neutral ratings | Slightly good ratings | Good ratings |
---|---|---|---|---|---|
product1 | 9 | 0 | 0 | 0 | 0 |
product2 | 0 | 2 | 2 | 2 | 0 |
product3 | 2 | 1 | 3 | 0 | 0 |
product4 | 1 | 1 | 1 | 1 | 1 |
product5 | 0 | 0 | 0 | 4 | 4 |
How do I transform a matrix such as this to show the overall rank of each product?
E.g. like this where 0 is the worst and 9 is the best:
index | Overall ratings |
---|---|
product1 | 1 |
product2 | 3 |
product3 | 2 |
product4 | 3 |
product5 | 4 |
CodePudding user response:
Something like this?
df['Bad ratings'] *= -2
df['Slightly bad ratings'] *= -1
df['Good ratings'] *= 2
df.set_index('index').sum(1).rank().sort_values()
Output:
index
product1 1.0
product3 2.0
product4 3.0
product2 4.0
product5 5.0