Numpy 使用np.where,但在条件为False时保持现有值

Numpy 使用np.where,但在条件为False时保持现有值,numpy,pandas,where,Numpy,Pandas,Where,我喜欢np.where,但从未完全掌握它 我有一个数据框,可以这样说: import pandas as pd import numpy as np from numpy import nan as NA DF = pd.DataFrame({'a' : [ 3, 0, 1, 0, 1, 14, 2, 0, 0, 0, 0], 'b' : [ 3, 0, 1, 0, 1, 14, 2, 0, 0, 0, 0], 'c'

我喜欢np.where,但从未完全掌握它

我有一个数据框,可以这样说:

import pandas as pd
import numpy as np
from numpy import nan as NA
DF = pd.DataFrame({'a' : [ 3, 0, 1, 0, 1, 14, 2, 0, 0, 0, 0],
                   'b' : [ 3, 0, 1, 0, 1, 14, 2, 0, 0, 0, 0],
                   'c' : [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   'd' : [5, 1, 2 ,1, 1 ,22, 30, 1, 0, 0, 0]})
cols = ['a', 'b', 'c', 'd']
condition = (DF[cols] == 0).all(axis=1)
for col in cols:
    DF[col] = np.where(condition, NA, ???)
现在我要做的是,当所有行值都为零时,用NaN值替换0值。关键的是,我希望在所有行值都不是零的情况下,保持行中的任何其他值

我想这样做:

import pandas as pd
import numpy as np
from numpy import nan as NA
DF = pd.DataFrame({'a' : [ 3, 0, 1, 0, 1, 14, 2, 0, 0, 0, 0],
                   'b' : [ 3, 0, 1, 0, 1, 14, 2, 0, 0, 0, 0],
                   'c' : [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   'd' : [5, 1, 2 ,1, 1 ,22, 30, 1, 0, 0, 0]})
cols = ['a', 'b', 'c', 'd']
condition = (DF[cols] == 0).all(axis=1)
for col in cols:
    DF[col] = np.where(condition, NA, ???)

我把它放在哪里了???为了表明如果条件为False,我不知道在那里放置什么值,我只想保留已经存在的任何值。np.where是否可能,或者我应该使用另一种技术?

对于这种任务,有一种
pandas.Series
方法(
where
)。一开始似乎有点落后,但从文档来看

系列。其中(cond,other=nan,inplace=False,axis=None,level=None, try_cast=False,raise_on_error=True)

返回与相同形状的对象 self,其对应条目来自self,其中cond为True 而其他的都是来自其他

所以,你的例子会变成

cols = ['a', 'b', 'c', 'd']
condition = (DF[cols] == 0).all(axis=1)
for col in cols:
    DF[col].where(~condition, np.nan, inplace=True)
但是,如果您要做的只是将特定列集合的全零行替换为
NA
,那么您可以这样做

DF.loc[condition, cols] = NA
编辑

为了回答您的原始问题,
np。其中
与其他数组操作相同,因此您将用
DF[col]
替换
,将示例更改为:

cols = ['a', 'b', 'c', 'd']
condition = (DF[cols] == 0).all(axis=1)
for col in cols:
    DF[col] = np.where(condition, NA, DF[col])

您可以这样做:

    array_binary = np.where(array[i]<threshold,0,1)
    array_sparse = np.multiply(array_binary,np.ones_like(array))

array\u binary=np。其中(array[i]建议的解决方案可行,但对于numpy数组,有一种更简单的方法,不使用数据帧

解决办法是:
np\u数组[np.where(condition)]=条件行的值\u\u true\u行

也许这实际上是最好的方法?”
DF.loc[DF[(DF[cols]==0.all(axis=1)]。index,cols]=NA