Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a function element-wise to two DataFrames

Tags:

python

pandas

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?

like image 901
Wang Avatar asked Dec 24 '22 20:12

Wang


2 Answers

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
like image 61
Psidom Avatar answered Dec 26 '22 09:12

Psidom


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

like image 42
Lili Blumenberg Avatar answered Dec 26 '22 09:12

Lili Blumenberg