将函数应用于来自一个df的行和来自另一个df

时间:2018-01-31 13:00:27

标签: python pandas numpy xarray

此问题在结构上类似于将行和列向量相乘以生成矩阵,然后汇总结果矩阵的行。

除了行向量外,每个元素都有两个值A和B,而在列向量中,每个元素都有两个值X和Y.而不是乘法,操作是评估A,B,X和Y的函数

以下代码实现了目标。但有没有办法在没有循环的情况下使用iterrows()?在实际问题中,行向量有数千个元素,列向量可能有数百万个。

from numpy import sin, cos, exp, nan 
from numpy.random import random

# Sample function that can operate on ndarrays
def myfun(a, b, x, y):
    return sin(a+x), exp(b+y) 

# sort of a "row vector"
df_ab = pd.DataFrame(random([2,6]), 
                     index=['A','B'],
                     columns=['AB%d'%i for i in range(6)])
# sort of a "column vector"
df_xy = pd.DataFrame(random([8,2]), 
                     columns=['X','Y'],
                     index=['XY%d'%i for i in range(8)])

# pre-add columns for the summarized results
df_xy['SUM_FUN0'] = nan
df_xy['SUM_FUN1'] = nan

# for each pair of values X,Y
for _, xy in df_xy.iterrows():
    # calculate myfun with each pair of values A,B
    funout0, funout1 = myfun(df_ab.loc['A'], df_ab.loc['B'], xy.X, xy.Y)
    # summarize and store the result
    xy['SUM_FUN0'] = funout0.sum()
    xy['SUM_FUN1'] = funout1.sum()    

1 个答案:

答案 0 :(得分:1)

这样的事情怎么样?我还没有测试过性能,但apply通常比iterrows略好。

import pandas as pd
from numpy import sin, cos, exp, nan, sum
from numpy.random import random
from numba import jit

# Sample function that can operate on ndarrays
@jit(nopython=True)
def myfun(a, b, x, y):
    return sum(sin(a+x)), sum(exp(b+y))

# sort of a "row vector"
df_ab = pd.DataFrame(random([2,6]), 
                     index=['A','B'],
                     columns=['AB%d'%i for i in range(6)])

# sort of a "column vector"
df_xy = pd.DataFrame(random([8,2]), 
                     columns=['X','Y'],
                     index=['XY%d'%i for i in range(8)])

A = df_ab.loc['A'].values
B = df_ab.loc['B'].values

df_xy['SUM_FUN0'], df_xy['SUM_FUN1'] = list(zip(*df_xy.apply(lambda x: myfun(A, B, x['X'], x['Y']), axis=1)))