@@ -231,22 +231,64 @@ Note, with the :ref:`advanced indexing <indexing.advanced>` ``ix`` method, you
231
231
may select along more than one axis using boolean vectors combined with other
232
232
indexing expressions.
233
233
234
- Indexing a DataFrame with a boolean DataFrame
235
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
234
+ Where and Masking
235
+ ~~~~~~~~~~~~~~~~~
236
236
237
- You may wish to set values on a DataFrame based on some boolean criteria
238
- derived from itself or another DataFrame or set of DataFrames. This can be done
239
- intuitively like so:
237
+ Selecting values from a Series with a boolean vector in the *[] *, returns a subset of the rows.
238
+ The method `where ` allows selection that preserves the original data shape (and is a copy).
240
239
241
240
.. ipython :: python
242
241
242
+ # return only the selected rows
243
+ s[s > 0 ]
244
+
245
+ # return a Series of the same shape as the original
246
+ s.where(s > 0 )
247
+
248
+ Selecting values from a DataFrame with a boolean critierion in the *[] *, that is the same shape as
249
+ the original DataFrame, returns a similary sized DataFrame (and is a copy). `where ` is used under the hood as the implementation.
250
+
251
+ .. ipython :: python
252
+
253
+ # return a DataFrame of the same shape as the original
254
+ # this is equiavalent to `df.where(df < 0)`
255
+ df[df < 0 ]
256
+
257
+ In addition, `where ` takes an optional `other ` argument for replacement of values where the
258
+ condition is False, in the returned copy.
259
+
260
+ .. ipython :: python
261
+
262
+ df.where(df < 0 , - df)
263
+
264
+ You may wish to set values based on some boolean criteria.
265
+ This can be done intuitively like so:
266
+
267
+ .. ipython :: python
268
+
269
+ s2 = s.copy()
270
+ s2[s2 < 0 ] = 0
271
+ s2
272
+
243
273
df2 = df.copy()
244
- df2 < 0
245
274
df2[df2 < 0 ] = 0
246
275
df2
247
276
248
- Note that such an operation requires that the boolean DataFrame is indexed
249
- exactly the same.
277
+ Furthermore, `where ` aligns the input boolean condition (ndarray or DataFrame), such that partial selection
278
+ with setting is possible. This is analagous to partial setting via `.ix ` (but on the contents rather than the axis labels)
279
+
280
+ .. ipython :: python
281
+
282
+ df2 = df.copy()
283
+ df2[ df2[1 :4 ] > 0 ] = 3
284
+ df2
285
+
286
+ `mask ` is the inverse boolean operation of `where `.
287
+
288
+ .. ipython :: python
289
+
290
+ s.mask(s >= 0 )
291
+ df.mask(df >= 0 )
250
292
251
293
252
294
Take Methods
0 commit comments