熊猫:如何动态计算所有列的PCT变化?

时间:2017-08-05 16:36:14

标签: python pandas

我通过使用以下命令得到以下pandas df,如何为AAL动态获取所有列的PCT更改,AAN ... 100更多

price['AABA_PCT_CHG'] = price.AABA.pct_change()

         AABA        AAL        AAN  AABA_PCT_CHG
0   16.120001   9.635592  18.836105           NaN
1   16.400000   8.363149  23.105881      0.017370
2   16.680000   8.460282  24.892321      0.017073
3   17.700001   8.829385  28.275263      0.061151
4   16.549999   8.839100  27.705627     -0.064972
5   15.040000   8.654548  27.754738     -0.091239

1 个答案:

答案 0 :(得分:1)

应用于数据框,如

In [424]: price.pct_change().add_suffix('_PCT_CHG')
Out[424]:
   AABA_PCT_CHG  AAL_PCT_CHG  AAN_PCT_CHG
0           NaN          NaN          NaN
1      0.017370    -0.132057     0.226680
2      0.017073     0.011614     0.077315
3      0.061151     0.043628     0.135903
4     -0.064972     0.001100    -0.020146
5     -0.091239    -0.020879     0.001773

In [425]: price.join(price.pct_change().add_suffix('_PCT_CHG'))
Out[425]:
        AABA       AAL        AAN  AABA_PCT_CHG  AAL_PCT_CHG  AAN_PCT_CHG
0  16.120001  9.635592  18.836105           NaN          NaN          NaN
1  16.400000  8.363149  23.105881      0.017370    -0.132057     0.226680
2  16.680000  8.460282  24.892321      0.017073     0.011614     0.077315
3  17.700001  8.829385  28.275263      0.061151     0.043628     0.135903
4  16.549999  8.839100  27.705627     -0.064972     0.001100    -0.020146
5  15.040000  8.654548  27.754738     -0.091239    -0.020879     0.001773