我有像这样的pandas数据框
>>> df['d']
date
2013-11-18 0
2013-11-19 0
2013-11-20 0
2013-11-21 -1
2013-11-22 0
2013-11-25 0
2013-11-26 0
2013-11-27 0
2013-11-29 0
2013-12-02 1
2013-12-03 0
2013-12-04 0
2013-12-05 0
2013-12-06 0
2013-12-09 0
2013-12-10 0
2013-12-11 0
2013-12-12 0
2013-12-13 -1
2013-12-16 0
2013-12-17 0
2013-12-18 0
2013-12-19 0
2013-12-20 0
2013-12-23 0
2013-12-24 0
2013-12-26 0
2013-12-27 0
2013-12-30 0
2013-12-31 1
2014-01-02 0
2014-01-03 0
2014-01-06 0
2014-01-07 0
2014-01-08 -1
Name: d, dtype: int64
我想将其更改为
>>> df['d']
date
2013-11-18 0
2013-11-19 0
2013-11-20 0
2013-11-21 -1
2013-11-22 -1
2013-11-25 -1
2013-11-26 -1
2013-11-27 -1
2013-11-29 -1
2013-12-02 1
2013-12-03 1
2013-12-04 1
2013-12-05 1
2013-12-06 1
2013-12-09 1
2013-12-10 1
2013-12-11 1
2013-12-12 1
2013-12-13 -1
2013-12-16 -1
2013-12-17 -1
2013-12-18 -1
2013-12-19 -1
2013-12-20 -1
2013-12-23 -1
2013-12-24 -1
2013-12-26 -1
2013-12-27 -1
2013-12-30 -1
2013-12-31 1
2014-01-02 1
2014-01-03 1
2014-01-06 1
2014-01-07 1
2014-01-08 -1
Name: d, dtype: int64
到目前为止,我使用此代码进行更改
>>> for i in range(len(df)):
... if i != 0 and df['d'][i] ==0:
... df['d'][i] = df['d'][i-1]
...
代码效率不高,有没有更有效的方法呢?我会假设必须有一些特殊的函数(例如apply,roll_apply)来迭代这些值,但我无法弄清楚这一点。任何帮助将不胜感激。
答案 0 :(得分:1)
只需你可以做到这一点
df['d'].replace(0 , method = 'ffill')