How to apply a function inside a class into a pandas Dataframe

The Dan

I'm trying to create a new column with a calculated value, this value is caclulated using methods inside a class. The class used to calculate is the following:

import functions_image_recognition as fir
class CompareImages():

    def __init__(self, url_1, url_2):
        self.img_url_1 = url_1
        self.img_url_2 = url_2

    def load_images(self, img_url, flag_color=False):
        req = urllib.request.urlopen(img_url)
        arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
        image = cv2.imdecode(arr, -1) # Load image as it is
        if not flag_color:
            return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Change color to greyscale
        else:
            return image # Original image

    def main_process_ssmi(self):
        imageA = self.load_images(self.img_url_1)
        imageB = self.load_images(self.img_url_2)
        (H, W) = imageA.shape
        imageB = cv2.resize(imageB, (W, H))
        (score, diff) = structural_similarity(imageA, imageB, full=True)
        result = float("{}".format(score))
        return result

The names of the columns in the dataframe are: seller_url,supplier_url

I want to create a new column in the dataframe with the result of applying the functions in the classes, and append the results to the original dataframe.

match_image = CompareImages(seller_url,supplier_url)
result = match_image.main_process_ssmi()

Dataset Sample

DarrylG

Using Apply you can use your existing class as follows.

Code

df['new_column'] = df.apply(lambda row: CompareImages(row['seller_url'], row['supplier_url']).main_process_ssmi(), axis = 1)

Explanation

We construct and object for each row and access the method main_process_ssmi.

In using:

row['seller_url'] and row['supplier_url'] 

The apply function passes row values, i.e. values for seller_url and supplier_url for each row.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to apply by chunks a function to pandas dataframe?

pandas DataFrame, how to apply function to a specific column?

How to apply a function against a dataframe in pandas?

How to apply a function with multiple conditions to pandas dataframe?

How to apply an if between function on a column in pandas' DataFrame

apply function to dataframe pandas

Pandas Dataframe apply() function

pandas shift inside apply function

Pandas - How to identify nans while inside the apply function?

How to apply function on a dataframe

Using apply function to Pandas dataframe

Apply a regex function on a pandas dataframe

Apply exec function to pandas DataFrame

Apply a custom function on a pandas dataframe

apply function returns a dataframe in pandas

Apply a Parsing Function to Pandas DataFrame

Apply function over a Pandas DataFrame

pandas dataframe groupby and then apply function

How to apply a class method to chunks of a pandas dataframe in parallel?

How to call pandas dataframe apply function to return two variables

How to apply a function to each row of one column in a pandas dataframe?

How to apply custom function to 2 columns of a pandas dataframe?

How to efficiently apply a function to each DataFrame of a Pandas Panel

How to apply euclidean distance function to a groupby object in pandas dataframe?

How to apply function to ALL columns of dataframe GROUPWISELY ? (In python pandas)

Pandas: How to use applymap/apply function with arguements to a dataframe without looping

How to apply function which refers to several columns to pandas dataframe?

How to speed up Pandas apply function to create a new column in the dataframe?

Pandas, how to apply changes dataframe with groupby() / conditional function calling?