1# replaces values with other where condition is False
2DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False)
3
4import pandas as pd
5df = pd.DataFrame({'values':[1,2,3,4]})
6
7df.where(df['values'] % 2 == 0, -1) # output : [-1, 2, -3, 4]
1In [20]: df
2Out[20]:
3 A B C
40 1 2 1
51 2 3 0
62 3 4 0
73 4 5 1
8
9# Note that df.C is a mask
10# Note that this is np.where, not df.where. That is different.
11In [21]: df['D'] = np.where(df.C, df.A, df.B)
12
13In [22]: df
14Out[22]:
15 A B C D
160 1 2 1 1
171 2 3 0 3
182 3 4 0 4
193 4 5 1 4
20