Skip to content

Commit 7853f3f

Browse files
author
Chang She
committed
DOC: more filtering examples. #746
1 parent 65a6d4e commit 7853f3f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

doc/source/indexing.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ Boolean indexing
181181

182182
.. _indexing.boolean:
183183

184+
Another common operation is the use of boolean vectors to filter the data.
185+
184186
Using a boolean vector to index a Series works exactly as in a numpy ndarray:
185187

186188
.. ipython:: python
@@ -207,6 +209,23 @@ select out rows where one or more columns have values you want:
207209
'c' : randn(7)})
208210
df2[df2['a'].isin(['one', 'two'])]
209211
212+
List comprehensions and ``map`` method of Series can also be used to produce
213+
more complex criteria:
214+
215+
.. ipython:: python
216+
217+
# only want 'two' or 'three'
218+
criterion = df2['a'].map(lambda x: x.startswith('t')
219+
220+
df2[criterion]
221+
222+
# equivalent but slower
223+
df2[[x.startswith('t') for x in df2['a']]]
224+
225+
# Multiple criteria
226+
df2[criterion & (df2['b'] == 'x')]
227+
228+
210229
Note, with the :ref:`advanced indexing <indexing.advanced>` ``ix`` method, you
211230
may select along more than one axis using boolean vectors combined with other
212231
indexing expressions.
@@ -336,7 +355,6 @@ default value.
336355
s.get('a') # equivalent to s['a']
337356
s.get('x', default=-1)
338357
339-
340358
.. _indexing.advanced:
341359
342360
Advanced indexing with labels

0 commit comments

Comments
 (0)