Book Image

Learning Pandas

By : Michael Heydt
Book Image

Learning Pandas

By: Michael Heydt

Overview of this book

Table of Contents (19 chapters)
Learning pandas
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Selecting rows of a DataFrame by Boolean selection


Rows can also be selected by using Boolean selection, using an array calculated from the result of applying a logical condition on the values in any of the columns. This allows us to build more complicated selections than those based simply upon index labels or positions.

Consider the following that is an array of all companies that have a price below 100.0.

In [45]:
   # what rows have a price < 100?
   sp500.Price < 100

Out[45]:
   Symbol
   MMM       False
   ABT        True
   ABBV       True
   ...
   ZMH       False
   ZION       True
   ZTS        True
   Name: Price, Length: 500, dtype: bool

This results in a Series that can be used to select the rows where the value is True, exactly the same way it was done with a Series or a NumPy array:

In [46]:
   # now get the rows with Price < 100
   sp500[sp500.Price < 100]

Out[46]:
                           Sector  Price  Book Value
   Symbol                                ...