0

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:

  1. 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'].

  2. 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'].

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
pyds_learner
  • 509
  • 4
  • 16

1 Answers1

0

Basically the idea is to compare every element in Ranked List with the given column and if it's present, You have to append them in the new list. This ensures that you do not disturb the already present ranking.

The above part can be easily achieved by a simple for loop.

The next part is to find all unique elements in Ranked List which are not present in new list and add both the lists.

Here's the code:

import pandas as pd
import numpy as np

#Create the Standard Ranked List
rank_list=['B','D','Z','A','C']

#Create the sample DataFrame
data={'Category 1':['A','C','G','D'],'Category 2':['B','A','D','Nothing'],'Category 3':['C','A','B','Nothing']}
df=pd.DataFrame(data)
#Replace all 'Nothing' Values with 'NaN'
df.replace('Nothing',np.NaN)

#Take the input from the user
print("Enter Category: ")
user_input=input()

#Create one list for filtering and one for the final result
present_values=[]
final_list=[]
#Compare all values in Standard Ranked List with df and if present, append to present_values
for value in rank_list:
       if value in df[user_input].values:
           present_values.append(value)

#Check for unique value not present in present_values   
for value in rank_list:
    if value not in present_values:
        final_list.append(value)

#Add both the lists
final_list=present_values+final_list

print("My final list is: ")
print(final_list)

Note: Don't forget to handle cases where user gives input as "category 1" but you need "Category 1"

The best way to handle this is make all your column names in lower case and whatever may be the input, convert everything in input to lower case.

Good Luck!

May the CODE be with you!

  • Is there a way to do it in a single loop ? – pyds_learner Feb 24 '19 at 06:18
  • Yes you can. Try something like Ordered Dict. You can also use a set to check for unique values buts sets are unordered, so ordered dict will be a better choice. Here's a link which may help you: [Ordered DIct in Python](https://stackoverflow.com/questions/39755464/python-unique-items-of-list-in-the-order-that-it-appears) –  Feb 24 '19 at 06:49