Pandas - Using `.rolling()` on multiple columns

前端 未结 2 804
庸人自扰
庸人自扰 2021-01-14 07:31

Consider a pandas DataFrame which looks like the one below

      A     B     C
0  0.63  1.12  1.73
1  2.20 -2.16 -0.13
2  0.97 -0.68  1.09
3 -0.         


        
2条回答
  •  囚心锁ツ
    2021-01-14 08:15

    One solution is to stack the data and then multiply your window size by the number of columns and slice the result by the number of columns. Also, since you want a forward looking window, reverse the order of the stacked DataFrame

    wsize = 3
    cols = len(df.columns)
    
    df.stack(dropna=False)[::-1].rolling(window=wsize*cols).quantile(0.75)[cols-1::cols].reset_index(-1, drop=True).sort_index()
    

    Output:

    0    1.12
    1    0.97
    2    0.97
    3     NaN
    4     NaN
    dtype: float64
    

    In the case of many columns and a small window:

    import pandas as pd
    import numpy as np
    
    wsize = 3
    df2 = pd.concat([df.shift(-x) for x in range(wsize)], 1)
    s_quant = df2.quantile(0.75, 1)
    
    # Only necessary if you need to enforce sufficient data. 
    s_quant[df2.isnull().any(1)] = np.NaN
    

    Output: s_quant

    0    1.12
    1    0.97
    2    0.97
    3     NaN
    4     NaN
    Name: 0.75, dtype: float64
    

提交回复
热议问题