[英]How to use pandas rolling apply with a simple custom function?
I have a function func
that I want to apply to consecutive rows of a pandas dataframe.我有一个 function
func
,我想将其应用于 pandas dataframe 的连续行。 However, I get a ValueError:
when I try to do it as below.但是,我得到一个
ValueError:
当我尝试如下操作时。
import numpy as np
import pandas as pd
def func(a: np.ndarray, b: np.ndarray) -> float:
"""Return the sum of sum of vectors a and b"""
return np.sum(a) + np.sum(b)
df = pd.DataFrame({"a": [1, 2, 3, 4, 5], "b": [10, 11, 12, 13, 14]})
df.rolling(window=2, axis=1).apply(func)
>>>
ValueError: Length of passed values is 2, index implies 5.
All I want to do is apply func
on a rolling basis to consecutive rows (which is why I chose window=2
above).我要做的就是将
func
滚动应用于连续行(这就是我在上面选择window=2
的原因)。 The snippet below is a manual implementation of this.下面的代码片段是对此的手动实现。
func(df.iloc[0, :].values, df.iloc[1, :].values)
>>> 24
func(df.iloc[1, :].values, df.iloc[2, :].values)
>>> 28
and so on.等等。
Note that the example I gave for func
is just for illustrative purposes - I know that that you could use a simple df.sum(axis=1) + df.shift(-1).sum(axis=1)
in this case.请注意,我为
func
提供的示例仅用于说明目的-我知道在这种情况下您可以使用简单的df.sum(axis=1) + df.shift(-1).sum(axis=1)
。 What I want to know is how you use rolling apply for custom functions in the general case.我想知道的是在一般情况下如何使用滚动申请自定义功能。
I guess this can be done with a few lines of code and an intermediate dataframe:我想这可以通过几行代码和一个中间 dataframe 来完成:
import numpy as np
import pandas as pd
def func(a: np.ndarray) -> float:
return np.sum(a)
df = pd.DataFrame({"a": [1, 2, 3, 4, 5], "b": [10, 11, 12, 13, 14]})
df_rolled = df.rolling(window=2).apply(func)
df["ab_rolled"] = [func([df_rolled["a"][i], df_rolled["b"][i]])
for i in range(0, len(df_rolled["a"]))]
print(df)
outputs:输出:
a b ab_rolled
0 1 10 NaN
1 2 11 24.0
2 3 12 28.0
3 4 13 32.0
4 5 14 36.0
This well could be an ugly code though.不过,这口井可能是一个丑陋的代码。 Sorry, it's the first time I use pandas.
抱歉,这是我第一次使用 pandas。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.