简体   繁体   English

根据另一列中指定的 window 大小在 pandas 中创建滚动 windows

[英]Create rolling windows in pandas based on window size specified in another column

I have a pandas dataframe that I'd like to sum on a rolling basis where the window is specified by another column.我有一个 pandas dataframe 我想滚动求和,其中 window 由另一列指定。

For example,例如,

values_to_sum values_to_sum window_size窗口大小 rolling_sum滚动总和
1 1 6 6 17 17
2 2 5 5 16 16
1 1 2 2 4 4
3 3 5 5 19 19
4 4 5 5 NaN
6 6 4 4 NaN
2 2 3 3 NaN
4 4 3 3 NaN

Trying to call the column window_size within the rolling function results in the error ValueError: window must be an integer .尝试在滚动 function 中调用列window_size会导致错误ValueError: window must be an integer

How can I call the column window_size on a row-by-row basis for the rolling function?对于滚动 function,如何逐行调用列window_size

With a list comprehension:使用列表理解:

df["rolling_sum"] = [np.nan 
                     if j + ws > len(df.index)
                     else df.values_to_sum.iloc[j: j+ws].sum()
                     for j, ws in enumerate(df.window_size)]

Put np.nan if the current index ( j ) plus window size ( ws ) exceeds the dataframe's length ( len(df.index) );如果当前索引( j )加上np.nan大小( ws )超过数据帧的长度( len(df.index) ),则放置 np.nan ; else get the window with iloc and sum it.否则用 iloc 得到ilocsum

to get要得到

   values_to_sum  window_size  rolling_sum
0              1            6         17.0
1              2            5         16.0
2              1            2          4.0
3              3            5         19.0
4              4            5          NaN
5              6            4          NaN
6              2            3          NaN
7              4            3          NaN

note: you can pre-define df_length = len(df.index) and use it to avoid looking for its length in the comprehension repeatedly.注意:您可以预先定义df_length = len(df.index)并使用它来避免在理解中重复寻找它的长度。

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

相关问题 Pandas rolling_max,在df列中指定了可变窗口大小 - Pandas rolling_max with variable window size specified in a df column Pandas:条件滚动 window 由另一个列元素? - Pandas: Conditional Rolling window by another column element? 如何在具有其他条件的熊猫中创建滚动窗口 - How to create a rolling window in pandas with another condition 基于不同列的可变窗口的熊猫滚动平均值 - Pandas rolling mean with variable window based on an different column 具有基于列总和的自定义回溯长度的熊猫滚动窗口 - Pandas rolling window with custom look back length based on column sum 熊猫:根据滚动窗口在数据框中创建新列 - Pandas: create a new column in a dataframe that is a function of a rolling window 基于另一列的熊猫滚动第二个最高值 - Pandas Rolling second Highest Value based on another column 熊猫:滚动意味着仅使用基于另一列的最后更新 - Pandas: Rolling mean using only the last update based on another column 滚动窗口,熊猫窗口大小重叠50% - rolling window with 50% overlapping on window size in pandas 尝试使用 Pandas 数据框中其他两列的 groupby 基于另一列创建新的滚动平均列时出错 - Error when trying to create new rolling average column based on another column using groupby of two other columns in pandas data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM