如何将设计用于一个数字的函数应用于pandas中的整个数据框?

时间:2022-07-01 21:39:01

I wrote a function that works as follows: matrix[24787][24788] = generate_value(source, 24787, 24788)

我写了一个函数如下:matrix [24787] [24788] = generate_value(source,24787,24788)

I want to generalize this to matrix = generate_value(source, <array of indices>, <array of columns>) and have the entire matrix filled in. How can I do this, short of writing two loops?

我想将此概括为matrix = generate_value(source, , )并填充整个矩阵。如何编写两个循环?

Edit: Here is generate_value -

编辑:这是generate_value -

def generate_value(source, id_a, id_b):
  intersection = pd.merge(source.get_group(id_a), source.get_group(id_b), how='outer', on='merge_field')
  intersection.val_x.fillna(value=0, inplace=True)
  intersection.val_y.fillna(value=0, inplace=True)
  intersection.total_value_differences = abs(intersection.val_x - intersection.val_y)
  return sum(intersection.total_value_differences)

I can rewrite it if necessary.

如有必要,我可以重写它。

1 个解决方案

#1


Looks impossible for this function in pandas at the moment.

目前在熊猫中看起来这个功能是不可能的。

#1


Looks impossible for this function in pandas at the moment.

目前在熊猫中看起来这个功能是不可能的。