Warren
Warren

Reputation: 1001

Apply a function element-wise to two DataFrames

How to apply a function z_ij = f(x_ij, y_ij) from DataFrame X and Y of the same size and save the result to DataFrame Z?

Upvotes: 10

Views: 8164

Answers (2)

Lili Blumenberg
Lili Blumenberg

Reputation: 126

just in case someone finds themselves here like I did, there is a function that does this now for pandas!

Z = X.combine(Y, lambda x, y: f(x, y))

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.combine.html

Upvotes: 11

akuiper
akuiper

Reputation: 214957

It depends on what kind of function you have, a lot of functions have already been vectorized for data frame, such as +-*/ etc, so for these functions, you can simply do Z = X + Y or Z = X - Y etc.

For a more general function, you can use numpy.vectorize to make a vectorized version of it and then apply to two data frames:

import numpy as np
import pandas as pd

X = pd.DataFrame([[1,2], [3,4]])
Y = pd.DataFrame([[2,1], [3,3]])
​
def f(x, y):                      # this is a demo function that takes in two ints and 
    return str(x) + str(y)        # concatenate them as str
​
vecF = np.vectorize(f)            # vectorize the function with numpy.vectorize
​
X
#   0   1
#0  1   2
#1  3   4

Y
#   0   1
#0  2   1
#1  3   3

pd.DataFrame(vecF(X, Y))          # apply the function to two data frames

#    0   1
#0  12  21
#1  33  43

Upvotes: 11

Related Questions