Python 如果满足条件,则基于最后一个非零值在列中填充零值

Python 如果满足条件,则基于最后一个非零值在列中填充零值,python,pandas,multiple-conditions,Python,Pandas,Multiple Conditions,考虑一个数据帧test=pd.DataFrame(数据=[0,0,1,0,0,0,-1,0,0,0,0,1,0,0],列=['holding']) 输出: +----------+ | Holdings | +----------+ | 0 | | 0 | | 1 | | 0 | | 0 | | 0 | | -1 | | 0 | | 0 | | 0 | |

考虑一个数据帧
test=pd.DataFrame(数据=[0,0,1,0,0,0,-1,0,0,0,0,1,0,0],列=['holding'])

输出:

+----------+
| Holdings |
+----------+
|        0 |
|        0 |
|        1 |
|        0 |
|        0 |
|        0 |
|       -1 |
|        0 |
|        0 |
|        0 |
|        1 |
|        0 |
|        0 |
+----------+
如果最后一个非零值等于1,我想用最后一个非零值替换所有零值。如果最后一个非零值等于-1,则无需将0替换为1

我尝试了
test['position\u holding']=test['holding'].replace(to\u replace=0,method='ffill')
,结果是

+------------------+
| position_holding |
+------------------+
|                0 |
|                0 |
|                1 |
|                1 |
|                1 |
|                1 |
|               -1 |
|               -1 |
|               -1 |
|               -1 |
|                1 |
|                1 |
|                1 |
+------------------+
在上表中,我唯一需要修正的是用-1填充零,这违反了第2个条件。我怎样才能做到这一点

Desired Output:
+------------------+
| position_holding |
+------------------+
|                0 |
|                0 |
|                1 |
|                1 |
|                1 |
|                1 |
|               -1 |
|                0 |
|                0 |
|                0 |
|                1 |
|                1 |
|                1 |
+------------------+
我的做法:

after = test.holding.eq(1)
before = test.holding.eq(-1)

test['pos_holding'] = test.holding.mask(test.holding.where(after|before).ffill()==1,1)
等效代码,稍微短一点:

mask = test.holding.where(test.holding != 0).ffill()
test['pos_holding'] = test.holding.mask(mask==1, 1)
输出:

    holding  pos_holding
0         0            0
1         0            0
2         1            1
3         0            1
4         0            1
5         0            1
6        -1           -1
7         0            0
8         0            0
9         0            0
10        1            1
11        0            1
12        0            1

不使用pandas或numpy,但一个简单的for循环也可以工作

for i in range(1, len(test)):
    if(test['holding'][i] == 0 and test['holding'][i-1] == 1):
        test['holding'][i] = 1
这应该行得通

test = pd.DataFrame(data = [0, 0, 1, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0], 
                    columns = ['holding'])
test['position_holding'] = test['holding'].replace(to_replace=0, method='ffill')

test["Diff"] = test["holding"]-test["position_holding"]
test.loc[test["Diff"]==1, 'position_holding']=0
然后您可以删除Diff列,该列现在已无用