Python 数据帧中的变量移位

Python 数据帧中的变量移位,python,pandas,dataframe,Python,Pandas,Dataframe,我想附加一个C列,其中包含B的滚动平均值(滚动周期=a) 例如,行索引(2)处的C值应为df.B.rolling(3).mean()=mean(10,20,30),行索引(4)处的C值应为df.B.rolling(2).mean()=mean(40,20)。。。但这一切都结束了 import pandas as pd df = pd.DataFrame({'A':[3,5,3,4,2,3,2,3,4,3,2,2,2,3], 'B':[10,20,30,40

我想附加一个
C
列,其中包含
B
的滚动平均值(滚动周期=
a


例如,行索引(2)处的
C
值应为
df.B.rolling(3).mean()=mean(10,20,30)
,行索引(4)处的
C
值应为
df.B.rolling(2).mean()=mean(40,20)
。。。但这一切都结束了

import pandas as pd

df = pd.DataFrame({'A':[3,5,3,4,2,3,2,3,4,3,2,2,2,3],
                   'B':[10,20,30,40,20,30,40,10,20,30,15,60,20,15]})

    A   B
0   3  10
1   5  20
2   3  30
3   4  40
4   2  20
5   3  30
6   2  40
7   3  10
8   4  20
9   3  30
10  2  15
11  2  60
12  2  20
13  3  15
解释
apply
遍历每列或每行。我们遍历每一行,因为我们使用参数
axis=1
(请参见
1
作为调用
apply
的第二个参数)。因此,
apply
的每次迭代都会传递表示当前行的pandas series对象。当前索引值位于行的
name
属性中。行对象的索引与
df
的列相同

因此,
df.index.get_loc(row.name)
查找当前索引值在
row.name
中的顺序位置<代码>行。A是该行的列
A

def crazy_apply(row):
    p = df.index.get_loc(row.name)
    a = row.A
    return df.B.iloc[p-a+1:p+1].mean()

df.apply(crazy_apply, 1)

0           NaN
1           NaN
2     20.000000
3     25.000000
4     30.000000
5     30.000000
6     35.000000
7     26.666667
8     25.000000
9     20.000000
10    22.500000
11    37.500000
12    40.000000
13    31.666667
dtype: float64