官网查看
pd.where()
DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, errors=‘raise’, try_cast=False, raise_on_error=None)
pd.mask()
np.where()
numpy.where(condition[, x, y])
pd.query()
注意****
df.where(m, -df) == np.where(m, df, -df)
df.where(m, -df) == df.mask(~m, -df)
df1.where(m, df2) 等价于 np.where(m, df1, df2).
栗子:
df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B'])
print(df)
print('***'*5)
print(df.where(df>2,-df))
结果:
A B
0 0 1
1 2 3
2 4 5
3 6 7
4 8 9
********
A B
0 0 -1
1 -2 3
2 4 5
3 6 7
4 8 9
(2&#x