简体   繁体   English

.apply() 将二元 numpy() lambda 函数应用于 Pandas DataFrame 的 .expanding() 窗口

[英].apply() a bivariate numpy() lambda function to an .expanding() window of a pandas DataFrame

Consider this simple code ( numpy.sum in loc_fun is a stand in for a more complicated bivariate function using numpy):考虑这个简单的代码( numpy.sum中的loc_fun是使用 numpy 的更复杂二元函数的替代品):

import pandas
import numpy


def loc_fun(A, B):
    return numpy.sum(A[:-1] > B[-1])


df = pandas.DataFrame(numpy.random.normal(0, 1, [100000, 2]), columns=['size_A', 'size_B']).cumsum(axis=0)
df.expanding(2).apply(lambda x: loc_fun(x.size_A.values, x.size_B.values))

The last line in the code above results in an error I cannot make sense of.上面代码中的最后一行导致我无法理解的错误。 Basically, I would like to apply loc_fun to an expanding() window of the values in the columns.基本上,我想将loc_fun应用于列中值的expanding()窗口。

In lambda x is a numpy.ndarray so You can not refer to column 'A-values' or 'B_values'.在 lambda 中,x 是一个 numpy.ndarray,因此您不能引用“A-values”或“B_values”列。

df.expanding(2).apply(lambda x: print(type(x)))

>><class 'numpy.ndarray'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM