October 2019
Beginner to intermediate
674 pages
16h 43m
English
The where() method is used to ensure that the result of Boolean filtering is the same shape as the original data. First, we set the random number generator seed to 100 so that the user can generate the same values, as shown next:
In [379]: np.random.seed(100) normvals=pd.Series([np.random.normal() for i in np.arange(10)]) normvals Out[379]: 0 -1.749765 1 0.342680 2 1.153036 3 -0.252436 4 0.981321 5 0.514219 6 0.221180 7 -1.070043 8 -0.189496 9 0.255001 dtype: float64 In [381]: normvals[normvals>0] Out[381]: 1 0.342680 2 1.153036 4 0.981321 5 0.514219 6 0.221180 9 0.255001 dtype: float64 In [382]: normvals.where(normvals>0) Out[382]: 0 NaN 1 0.342680 2 1.153036 3 NaN 4 0.981321 5 0.514219 6 0.221180 7 NaN 8 NaN 9 ...
Read now
Unlock full access