Home > Back-end >  writing a python function to get desired value from csv
writing a python function to get desired value from csv

Time:06-19

label1 label2 value
img1_1 img1_2 2
img1_1 img2_1 3
img1_1 img2_2 4
img1_1 img3_1 4
img1_1 img3_2 4
img1_2 img1_1 2
img1_2 img2_1 3
img1_2 img2_2 3
img1_2 img3_1 7
img1_2 img3_2 4
img2_1 img1_1 3
img2_1 img1_2 3
img2_1 img2_2 3
img2_1 img3_1 5
img2_1 img3_2 6
img2_2 img1_1 4
img2_2 img1_2 3
img2_2 img2_1 3
img2_2 img3_1 4
img2_2 img3_2 5
img3_1 img1_1 4
img3_1 img1_2 7
img3_1 img2_1 5
img3_1 img2_2 4
img3_1 img3_2 4
img3_2 img1_1 6
img3_2 img1_2 4
img3_2 img2_1 6
img3_2 img2_2 5
img3_2 img3_1 4

From that table I want to write a python script that will return a value like this:

def compute(img1,img2):
   comb1=(img1_1,img2_1) (img1_1,img2_2)
   comb2=(img1_2,img2_1) (img1_2,img2_2)
   return minimum(comb1,comb2)

I want the function to be also work with other combination. For example it should work similar for compute(img2,img3), compute(img1,img3). In another word,

  def compute(img2,img3):
       comb1=(img2_1,img3_1) (img2_1,img3_2)
       comb2=(img2_2,img3_1) (img2_2,img3_2) 
       return minimum(comb1,comb2)

What will be the easiest way to write that type of function? or how can I utilize pandas to tackle this?

I also want to create a for loop to iterate the compute function for every possible variable and save the result in another CSV like this:

func ans
compute(img2,img3) 9
compute(img1,img3) 8
compute(img1,img2) 6

The hardship I am facing is writing the compute function which will calculate the two comb variable as I mentioned. I just want the compute function to be scalable. So that iterating the compute function through a loop gives me as many combinations as possible for the big dataset.

CodePudding user response:

import pandas as pd

def compute(img1, img2):
    ferst1 = img1   '_1'
    ferst2 = img1   '_2'
    second1 = img2   '_1'
    second2 = img2   '_2'
    comb1 = df.loc[(df['label1'] == ferst1) & (df['label2'] == second1), 'value'].values[0]\
              df.loc[(df['label1'] == ferst1) & (df['label2'] == second2), 'value'].values[0]
    comb2 = df.loc[(df['label1'] == ferst2) & (df['label2'] == second1), 'value'].values[0]\
              df.loc[(df['label1'] == ferst2) & (df['label2'] == second2), 'value'].values[0]

    return min(comb1, comb2)

print(compute('img1', 'img2'))

From the arguments, four variables are created in the function with the addition of the strings '_1' and '_2'. If I guess correctly, the argument on the left looks for a match in the label1 column, the one on the right in label2. If this is not the case, please let me know. If you need to look for each argument in both columns, then there will be more than one match, which of the obtained values then choose.

  • Related