Given a list of cars and their top speeds, MPG and car cost. I want to rank them. With Speed a 'weight' of 50%, MPG 'weight' of 30% and car cost 20%.
The faster the car, the better.. The higher the MPG, the better... The lower the COST, the better...
What formula can I use to rank the cars in order, based on this criteria?
So given this list. How can I rank them?
CAR SPEED MPG COST
A 135 20 50,000
B 150 15 60,000
C 170 18 80,000
D 120 30 40,000
What software library can i use to do this ? Python? Numpy?
thanks
CodePudding user response:
You can score the different rows with simple arithmetic. Since COST
is inversely proportioned, we'll consider 1/COST
instead. If COST
can ever be zero, you could also subtract it instead of use the inverse.
import pandas as pd
df = pd.DataFrame({'CAR': ['A', 'B', 'C', 'D'],
'SPEED': [135, 150, 170, 120],
'MPG': [20, 15, 18, 30],
'COST': [50.0, 60.0, 80.0, 40.0]})
score = 0.5*df["SPEED"] 0.3*df["MPG"] 0.2/df["COST"]
df.loc[score.sort_values(ascending=False).index]
CAR SPEED MPG COST
2 C 170 18 80.0
1 B 150 15 60.0
0 A 135 20 50.0
3 D 120 30 40.0
CodePudding user response:
You should use a Python library called "pandas", where you can manipulate your data, Try something like this:
import pandas as pd
df = pd.read_csv("your_csv_file.csv")
scores = []
for index, row in df.iterrows():
mpg = row['MPG']
speed = row['SPEED']
cost = row['COST']
score = (int(mpg) / df['MPG'].max() * 50) (int(speed) / df['SPEED'].max() * 30) (df['COST'].max() / int(cost) * 20)
scores.append(score)
df['SCORE'] = scores
df = df.sort_values(by=['SCORE'], ascending=False)
It creates a new column with its associated score:
CAR SPEED MPG COST SCORE
3 D 120 30 40.0 111.176471
0 A 135 20 50.0 89.156863
2 C 170 18 80.0 80.000000
1 B 150 15 60.0 78.137255