numpy.where(condition,x,y)
- 输出什么取决于此处的condition(条件)。
- 若condition为True,则输出x,否则(False)输出y。
- 举个例子:
>>>a=pd.Series([np.nan,2.5,0.0,3.5,4.5,np.nan],
index=['f','e','d','c','b','a'])
>>>b=pd.Series([0.,np.nan,2.,np.nan,np.nan,5.],
index=['a','b','c','d','e','f'])
>>>a
f NaN
e 2.5
d 0.0
c 3.5
b 4.5
a NaN
dtype: float64
>>>b
a 0.0
b NaN
c 2.0
d NaN
e NaN
f 5.0
dtype: float64
#此处,pandas.isnull()函数判断a中的值是否为缺失值,1、True——where()返回0.(在b中)
#2、False——where()返回2.5(在a中)......
>>>np.where(pd.isnull(a),b,a)
array([0. , 2.5, 0. , 3.5, 4.5, 5. ])