通过Pandas中的循环移动所有列

时间:2017-10-30 14:51:17

标签: python pandas dataframe

我想通过循环移动我的Dataframe。我有以下数据框;

           A          B     
0         0.0         101   
1         0.0  101.996163   
2         0.0  209.987279   
3         0.0  168.605494   
4         0.0  138.245242 

我可以使用以下代码移动每一列;

for col in df.columns:
    df["%s_%s"%(col,-1)] = df[col].shift(1)

我从上面的代码得到的输出是;

           A          B            A_-1    B_-1
0         0.0  101.996163          NaN     NaN
1         0.0  209.987279          0       101.996163
2         0.0  168.605494          0       209.987279
3         0.0  138.245242          0       168.605494

但是如果我必须创建额外的列,每个列代表不同的时间滞后,例如想把每一列调整10?有没有办法做到这一点(可能通过使用循环)?

1 个答案:

答案 0 :(得分:0)

使用其他for

for col in df.columns:
    for i in range(1,5):
        df["%s_%s_%s"%(col,i,-1)] = df[col].shift(i)

print (df)
     A           B  A_1_-1  A_2_-1  A_3_-1  A_4_-1      B_1_-1      B_2_-1  \
0  0.0  101.000000     NaN     NaN     NaN     NaN         NaN         NaN   
1  0.0  101.996163     0.0     NaN     NaN     NaN  101.000000         NaN   
2  0.0  209.987279     0.0     0.0     NaN     NaN  101.996163  101.000000   
3  0.0  168.605494     0.0     0.0     0.0     NaN  209.987279  101.996163   
4  0.0  138.245242     0.0     0.0     0.0     0.0  168.605494  209.987279   

       B_3_-1  B_4_-1  
0         NaN     NaN  
1         NaN     NaN  
2         NaN     NaN  
3  101.000000     NaN  
4  101.996163   101.0  

如果需要将所有列移动一些N,例如3

for col in df.columns:
    df["%s_%s"%(col,-1)] = df[col].shift(3)

print (df)
     A           B  A_-1        B_-1
0  0.0  101.000000   NaN         NaN
1  0.0  101.996163   NaN         NaN
2  0.0  209.987279   NaN         NaN
3  0.0  168.605494   0.0  101.000000
4  0.0  138.245242   0.0  101.996163