这个问题在结构上就像将行向量和列向量相乘以生成矩阵,然后汇总所得矩阵的行。

除了在行向量中,每个元素都有两个值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()

最佳答案

这样的事情怎么样?我还没有测试性能,但是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)))

07-24 17:50