Boolean indexing

We use Boolean indexing to filter or select parts of the data. The operators are as follows:

Operators

Symbol

OR

|

AND

&

NOT

~

 

These operators must be grouped using parentheses when used together. Using the earlier DataFrame from the previous section, here we display the trading dates for which NASDAQ closed above 4,300:

  In [311]: sharesIndexDataDF.ix[(sharesIndexDataDF['PriceType']=='close') & 
                         (sharesIndexDataDF['Nasdaq']>4300) ]
Out[311]: PriceType Nasdaq S&P 500 Russell 2000 TradingDate 2014/02/27 close 4318.93 1854.29 1187.94 2014/02/28 close 4308.12 1859.45 1183.03 2 rows × 4 columns

You can also create Boolean conditions in which you use arrays to filter out parts of the data, as shown in the following code:

highSelection=sharesIndexDataDF['PriceType']=='high'  NasdaqHigh=sharesIndexDataDF['Nasdaq']<4300  sharesIndexDataDF.ix[highSelection & NasdaqHigh]
Out[316]: TradingDate PriceType Nasdaq S&P 500 Russell 2000 2014/02/21 high 4284.85 1846.13 1168.43

Thus, the preceding code snippet displays the only date in the dataset for which the NASDAQ Composite index stayed below the 4,300 level for the entire trading session.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset