Use custom function on individual columns

Viewed 31

I have two data frames that could look something like this:

df1 =

                               val1           val2            val3           val4
date
2020-09-08 00:00:00+02:00      0.00           -700.07         21.700         3078.05
2020-09-08 01:00:00+02:00      0.00           -433.82         24.145         2585.33
2020-09-08 02:00:00+02:00      0.00           -160.87         26.145         2096.54
2020-09-08 03:00:00+02:00      0.00            -11.75         22.145         1679.15
2020-09-08 04:00:00+02:00      0.00           -155.17         28.145         1107.15


df2 =

                               val1           val2            val3           val4
date
2020-09-09 00:00:00+02:00      10.00          -400.07         11.700         1078.05
2020-09-09 01:00:00+02:00      20.00          -233.82         14.145         1585.33
2020-09-09 02:00:00+02:00      40.00          -560.87         16.145         1096.54
2020-09-09 03:00:00+02:00      44.00          -161.75         12.145         2679.15
2020-09-09 04:00:00+02:00      13.00          -555.17         18.145         2107.15

I then have a function that looks something like this:

def my_func(df1, df2):
    scores = numpy.mean(abs(df1 - df2), axis=0)

    return scores

And scores will return an array of 4 values.

This works because Pandas allow you to subtract data frames like this, which is great. But now my problem.

I also have a function from a package I did not create. It basically takes two lists as argument, and then do some calculations. It would maybe look something like:

def other_func(list1, list2):
    scores = package_func(list1, list2))

    return scores

This function only returns one value for each two lists, hence I would need to iterate over each column to get the same output as in my_func, e.g:

def other_func(df1, df2):
    scores = [package_func(df1[col_name], df2[col_name])) for col_name in for df1.columns] 

    return scores

And maybe that is the way to do it ? I was just wondering if there was some trick to just do all columns at once without the need to iterate through the columns ?

1 Answers

Given the following toy package_func and dataframes:

import pandas as pd

def package_func(list1, list2):
    return max(list2) - max(list1)

df1 = pd.DataFrame({"val1": [1, 5, 3], "val2": [4, 2, 6], "val3": [8, 7, 9]})
df2 = pd.DataFrame({"val1": [14, 7, 5], "val2": [9, 3, 8], "val3": [1, 0, 3]})

Here is one way to do it:

scores = list(map(package_func, df1.T.values, df2.T.values))

print(scores)
# Output
[9, 3, -6]
Related