简体   繁体   English

如何更有效地修改熊猫数据框列

[英]how to modify a pandas dataframe column more efficient

I have a pandas dataframe like this 我有一个像这样的熊猫数据框

>>> 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

I would like to change it to 我想将其更改为

>>> 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

So far I used this code to make the change 到目前为止,我已使用此代码进行更改

>>> for i in range(len(df)):
...     if i != 0 and df['d'][i] ==0:
...             df['d'][i] = df['d'][i-1]
...

The code is not that efficient, Is there a more efficient way to do this? 代码效率不高,有没有更有效的方法? I would assume there must be some special function (such as apply, roll_apply) to iterate through these values, but I couldn't figure that out. 我认为必须有一些特殊的函数(例如apply,roll_apply)来遍历这些值,但是我无法弄清楚。 Any help would be appreciated. 任何帮助,将不胜感激。

只需您即可做到

df['d'].replace(0 , method = 'ffill')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM