简体   繁体   English

比较并替换数据帧中的值

[英]compare and replace the value from a data-frame

I am new to pandas and dataframe .我是 pandas 和 dataframe 的新手。

Here I have a Y which is like这里我有一个 Y 就像

     col_name
       2000
       2000
       2300
       2664
       2300
       51200

Now, In this I have an array which is like现在,在这个我有一个数组,就像

imp_features = [2000,2300]

So, Now I am trying to replace the values to 0 if the value is not from the array.所以,如果值不是来自数组,现在我试图将值替换为 0。

Y[~Y.isin(imp_features)] = 0

Now, Before doing this , I am trying if the value is 2664 then replace it with 2300 like this.现在,在执行此操作之前,我尝试将值设为 2664,然后像这样将其替换为 2300。

I tried like this我试过这样

 y = pd.Series(np.where(y==2664, 2300,y))

But not working.但不工作。 can any one help me wit this.任何人都可以帮助我了解这一点。

Try:尝试:

Y = Y.replace(2664, 2300)
Y = Y.where(Y['col_name'].isin(imp_features), 0)
Y 

   col_name
0      2000
1      2000
2      2300
3      2300
4      2300
5         0

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

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