![pandas numpy where mask 关系总结_pandas](//dev-img.mos.moduyun.com/20231023/e30950eb-4a2b-4fa6-ab4f-e2a5e3676b5f.png)
df_1 = df_.iloc[:, columns_select_index]
df_2 = df_1.copy()
# 方法1
df_2['铝价1'] = np.where(
df_1['非锁铝价-币种'].isna(),
df_1['非锁铝价'],
df_1['非锁铝价-币种']
)
# 方法2
df_2['铝价2'] = df_1['非锁铝价-币种'].mask(
df_1['非锁铝价-币种'].isna(),
df_1['非锁铝价']
)
df_2
# import numpy as np
seri = pd.Series([1,2,3,4])
np.where(
seri % 2 == 0
# x 默认为no
# y 默认为原值
) # either both or neither of x and y should be given
# np.ma.where效果和机理貌似一致
![pandas numpy where mask 关系总结_pandas_02](//dev-img.mos.moduyun.com/20231023/5935316f-a583-44c9-8d32-47c86a1dcc3c.png)
ser_1 = pd.Series(range(5))
ser_2 = pd.Series([True, False])
ser_1.to_frame(name='ser_1').query(
"ser_1 in @ser_2"
)