File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -181,6 +181,8 @@ Boolean indexing
181
181
182
182
.. _indexing.boolean :
183
183
184
+ Another common operation is the use of boolean vectors to filter the data.
185
+
184
186
Using a boolean vector to index a Series works exactly as in a numpy ndarray:
185
187
186
188
.. ipython :: python
@@ -207,6 +209,23 @@ select out rows where one or more columns have values you want:
207
209
' c' : randn(7 )})
208
210
df2[df2[' a' ].isin([' one' , ' two' ])]
209
211
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
+
210
229
Note, with the :ref:`advanced indexing < indexing.advanced> ` `` ix`` method, you
211
230
may select along more than one axis using boolean vectors combined with other
212
231
indexing expressions.
@@ -336,7 +355,6 @@ default value.
336
355
s.get(' a' ) # equivalent to s['a']
337
356
s.get(' x' , default = - 1 )
338
357
339
-
340
358
.. _indexing.advanced:
341
359
342
360
Advanced indexing with labels
You can’t perform that action at this time.
0 commit comments