mercredi 29 septembre 2021

Replicate H Lookup Functionality in Python Pandas for Dataframe

Question - How to best attempt the problem as nested loops is slowing the process and not giving the desired result

Same thing can be done in the Excel using Hlookup, but as it is a repetitive exercise, I need to automate it

I have the below lookup table.

lookup = pd.DataFrame({'Fruit': ['Apple','Mango','Guava'],'Rate':[20,30,25],
               'Desc':['Apple rate is higher', 'Mango rate is higher', 'Guava rate is higher']})

My objective is to mark desc in my input data wherever the rate is greater than as mentioned in lookpup table

input_data = pd.DataFrame({'Id':[1,2,3,4,5], 'Apple':[24,27,30,15,18], 'Mango':[28,32,35,12,26],
                       'Guava':[20,23,34,56,23]})

Expected Output data sample -

output_data = pd.DataFrame({'Id':[1,2,3,4,5], 'Apple':[24,27,30,15,18], 'Mango':[28,32,35,12,26],
                       'Guava':[20,23,34,56,23], 'Desc':['Apple rate is higher', 
                                                         'Apple rate is higher, Mango rate is higher',
                                                         'Apple rate is higher, Mango rate is higher, Guava rate is higher',
                                                         'Guava rate is higher', '']})

I have tried using the loop and created two list which gives me the index and value to be inserted. I am confused how to progress to next step and it seems a very slow method as I have multiple nested loops

for i in range(0,len(lookup)):
var1 = lookup['Fruit'][i]
value1 = lookup['Rate'][i]
desc1 = lookup['Desc'][i]

for j in range(0, len(input_data.columns)):
    var2 = input_data.columns[j]
    a=[]
    b=[]

    if var1 == var2:
        for k in range(0, len(input_data)):
            if input_data[var2][k] > value1:
                a.append(desc1)
                b.append(k)
        print (a)
        print (b)

Output of my code

['Apple rate is higher', 'Apple rate is higher', 'Apple rate is higher'] [0, 1, 2]

['Mango rate is higher', 'Mango rate is higher'] [1, 2]

['Guava rate is higher', 'Guava rate is higher'] [2, 3]

Aucun commentaire:

Enregistrer un commentaire