我有这样的系列
s = pd.Series([[1,2,3],[1,2,3],np.nan,[1,2,3],[1,2,3],np.nan])
我只想让NaN
被[0,0,0]
取代。
我试过了
s.fillna([0,0,0]) # TypeError: "value" parameter must be a scalar or dict, but you passed a "list"
s[s.isna()] = [[0,0,0],[0,0,0]] # just replaces the NaN with a single "0". WHY?!
s.fillna("NAN").replace({"NAN":[0,0,0]}) # ValueError: NumPy boolean array indexing assignment cannot
#assign 3 input values to the 2 output values where the mask is true
s.fillna("NAN").replace({"NAN":[[0,0,0],[0,0,0]]}) # TypeError: NumPy boolean array indexing assignment
# requires a 0 or 1-dimensional input, input has 2 dimensions
我真的不明白,为什么第一种方法行不通(也许我得到了第一种方法,但第二种方法我不能把头绕在头上)。
感谢这的问答,我们可以通过
is_na = s.isna()
s.loc[is_na] = s.loc[is_na].apply(lambda x: [0,0,0])
但是由于apply
的速度往往很慢,我无法理解,为什么我们不能像上面那样使用replace
或切片
https://stackoverflow.com/questions/72257351
复制相似问题