更新熊猫列以将其替换为np.nan(如果该值出现一次),然后重置另一个值occus

时间:2018-12-09 18:58:13

标签: python pandas python-2.x

标题非常混乱,所以让我解释一下。我有一个熊猫专栏:

x   | desired x
1.5 | 1
1   | 1
1   | 1         
1   | 1
1   | 1
0   | 0
0   | 0
0   | 0
0   | 0
1   | 0
0   | 0
-1.5|-1
-1  |-1
-1  |-1
-1  |-1
0   | 0  
0   | 0
0   | 0
0   | 0
-1  | 0
0   | 0
0   | 0
1.5 | 1

...

当前,我已经使用itertuples解决了这个问题:

 currval = np.nan
for idx in df.itertuples():
    if idx[33] == 1.5: 
        currval = 1
    elif idx[33] == -1.5:
        currval = -1
    elif idx[32] <> "":
        currval = np.nan
    else: 
        next
    df.loc[idx.Index,'refPos2'] = currval

但是,这段代码太慢了,想知道是否有人对如何向量化提出了想法。

谢谢!

1 个答案:

答案 0 :(得分:0)

我从评论中了解到的问题陈述,这是解决方案:

View myView = findViewById(R.id.my_view);
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
    // ... Respond to touch events
    return true;
}});