代码之家  ›  专栏  ›  技术社区  ›  exan

DataFrame.Apply()忽略第一列

  •  0
  • exan  · 技术社区  · 8 年前

    问题

    MovingAverage 它计算一系列的移动平均数。现在我尝试使用 apply() 应用()

    数据

    df1 = pd.DataFrame({'Action':np.random.randint(0, 11, 10),
                       'Adventure':np.random.randint(0, 11, 10),
                       'Comedy':np.random.randint(0, 11, 10),
                       'Fantasy':np.random.randint(0, 11, 10),
                       'Drama':np.random.randint(0, 11, 10)})
    

    # function: MovingAverage()
    def MovingAverage(xSeries, MA_Limit = 1):
        MA_Series = pd.Series(np.zeros(len(xSeries), dtype=float))
        for i in range(MA_Limit, len(xSeries)):
            MA_Series[i] = np.mean(xSeries[i-MA_Limit:i])
    
        j = MA_Limit
        while j > 0:
            MA_Series[j-1] = np.mean(xSeries[0:j])
            j = j-1
    
        return MA_Series
    

    df.apply(MovingAverage, axis=1, MA_Limit=5)
    

    电流输出

    Current Output

    应用于每列的移动平均滤波器

    2 回复  |  直到 8 年前
        1
  •  0
  •   jezrael    8 年前

    看起来需要 rolling 具有 axis=1 对于进程行:

    np.random.seed(456)
    
    df1 = pd.DataFrame({'Action':np.random.randint(0, 11, 10),
                       'Adventure':np.random.randint(0, 11, 10),
                       'Comedy':np.random.randint(0, 11, 10),
                       'Fantasy':np.random.randint(0, 11, 10),
                       'Drama':np.random.randint(0, 11, 10)})
    print (df1)
       Action  Adventure  Comedy  Fantasy  Drama
    0       5          5       0        3     10
    1       9          2       9        3      5
    2      10          4      10        5      1
    3      10          2       8       10      6
    4       4          2       2        6      1
    5       5          8       3        6      5
    6       7          4       6        3     10
    7       1          8       7        8      9
    8       8          5       0        9      1
    9       3          6       0        6      4
    
    #also first column is noct changed
    df2 = df1.rolling(3, min_periods=1, axis=1).mean()
    print (df2)
       Action  Adventure    Comedy   Fantasy     Drama
    0     5.0        5.0  3.333333  2.666667  4.333333
    1     9.0        5.5  6.666667  4.666667  5.666667
    2    10.0        7.0  8.000000  6.333333  5.333333
    3    10.0        6.0  6.666667  6.666667  8.000000
    4     4.0        3.0  2.666667  3.333333  3.000000
    5     5.0        6.5  5.333333  5.666667  4.666667
    6     7.0        5.5  5.666667  4.333333  6.333333
    7     1.0        4.5  5.333333  7.666667  8.000000
    8     8.0        6.5  4.333333  4.666667  3.333333
    9     3.0        4.5  3.000000  4.000000  3.333333
    

    #first row is not changed, because rolling mean per columns (default axis=0)
    df3 = df1.rolling(3, min_periods=1).mean()
    print (df3)
         Action  Adventure    Comedy   Fantasy      Drama
    0  5.000000   5.000000  0.000000  3.000000  10.000000
    1  7.000000   3.500000  4.500000  3.000000   7.500000
    2  8.000000   3.666667  6.333333  3.666667   5.333333
    3  9.666667   2.666667  9.000000  6.000000   4.000000
    4  8.000000   2.666667  6.666667  7.000000   2.666667
    5  6.333333   4.000000  4.333333  7.333333   4.000000
    6  5.333333   4.666667  3.666667  5.000000   5.333333
    7  4.333333   6.666667  5.333333  5.666667   8.000000
    8  5.333333   5.666667  4.333333  6.666667   6.666667
    9  4.000000   6.333333  2.333333  7.666667   4.666667
    
        2
  •  0
  •   quest    8 年前

    为了应用移动平均线,只需使用 rolling() 功能。对于2号窗口,

    df1.rolling(window=[![enter image description here][1]][1]2).mean()