I have a ranked list of elements:
['B','D','Z','A','C']
It means 'B' is Rank 1, 'E' is Rank 2 and so on.
Now, I have a dataframe df
:
Category 1 Category 2 Category 3
A B C
C A A
G D B
D Nan Nan
Based on user input, let's say the user has inputted "Category 1". Accordingly I need to go the dataframe df
and search for a column name matching the user input (in our case "Category 1").
And check if any of our values in the ranked list are available in that column "Category 1" and output a re-ranked list:
['D','A','C','B','Z']
Logic behind for implementation:
Go through the initial ranked list of elements and see if any elements are available in "Category 1". If it's available, then rank them as 1st, 2rd, and so on based on their initial ranking (maintaining the ranking order in the initial ranked list).
For example: From the initial ranked list, elements 'D', 'A', 'C' are available in "Category 1". Now get their respective ranks from the initial rank and re-rank them in the new list from the top. So our top 3 elements in the re-ranked list will be
['D','A','C']
.Check for the remaining elements in the initial ranked list - 'B', 'Z'. Since they are not in "Category 1" we need to rank them last maintaining their respective ranking order. So our final re-ranked list will be
['D','A','C','B','Z']
.