python中已经有不少默认的向量化操作的函数如:±×/之类
那么如何自定义向量化操作函数呢?
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