Reputation: 637
I want to create a weighted ranking for a data frame based on some other variables (see example below). However, a 0 means that the data for the score are missing and the row should therefore get the lowest ranking value. I know about the na_option
, but I would prefer not to change the values in the Score
columns. Since I want to try different boolean combinations the code has to be flexible to ascending and descending ranking.
Id Score1 Score2 Score3
1 5 0 8
2 -4 2 6
3 3 1 5
4 0 -4 -3
w1, w2, w3 = 0.4, 0,3, 0.3
boolean1, boolean2, boolean3 = True, False, True
df['tmp_rank1'] = df[Score1].rank(ascending=boolean1)
df['tmp_rank2'] = df[Score2].rank(ascending=boolean2)
df['tmp_rank3'] = df[Score3].rank(ascending=boolean3)
df['final_rank'] = df['tmp_rank1'] * w1 + df['tmp_rank2'] * w2 + df['tmp_rank3'] * w3
Upvotes: 1
Views: 80
Reputation: 29635
IIUC, I think you can use mask
to replace 0 with nan before creating the rank
and na_option
, but never assign the nan back to the original column
w1, w2, w3 = 0.4, 0.3, 0.3
boolean1, boolean2, boolean3 = True, False, True
df['tmp_rank1'] = df['Score1'].mask(df['Score1'].eq(0))\
.rank(ascending=boolean1, na_option='bottom')
df['tmp_rank2'] = df['Score2'].mask(df['Score2'].eq(0))\
.rank(ascending=boolean2, na_option='bottom')
df['tmp_rank3'] = df['Score3'].mask(df['Score3'].eq(0))\
.rank(ascending=boolean3, na_option='bottom')
df['final_rank'] = df['tmp_rank1'] * w1 + df['tmp_rank2'] * w2 + df['tmp_rank3'] * w3
Upvotes: 1