{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Настройка библиотеки pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# импортируем numpy и pandas\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# импортируем datetime\n",
    "import datetime\n",
    "from datetime import datetime, date\n",
    "\n",
    "# задаем некоторые настройки pandas, регулирующие\n",
    "# формат вывода\n",
    "pd.set_option('display.notebook_repr_html', False)\n",
    "pd.set_option('display.max_columns', 8)\n",
    "pd.set_option('display.max_rows', 10)\n",
    "pd.set_option('display.width', 80)\n",
    "\n",
    "# импортируем matplotlib для построения графиков\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Создание объекта DataFrame на основе результатов функций NumPy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   0\n",
       "0  1\n",
       "1  2\n",
       "2  3\n",
       "3  4\n",
       "4  5"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем датафрейм из одномерного\n",
    "# массива NumPy\n",
    "pd.DataFrame(np.arange(1, 6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    0   1\n",
       "0  10  11\n",
       "1  20  21"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем датафрейм из двумерного\n",
    "# массива NumPy\n",
    "df = pd.DataFrame(np.array([[10, 11], [20, 21]]))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=2, step=1)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# получаем индекс столбцов\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   Missoula  Philadelphia\n",
       "0        70            71\n",
       "1        90            91"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# задаем имена столбцов\n",
    "df = pd.DataFrame(np.array([[70, 71], [90, 91]]),\n",
    "                  columns=['Missoula', 'Philadelphia'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# сколько строк?\n",
    "len(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# какова размерность датафрейма?\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Создание объекта DataFrame с помощью питоновского словаря и объектов Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   Missoula  Philadelphia\n",
       "0        70            90\n",
       "1        71            91"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создание датафрейма с помощью\n",
    "# питоновского словаря\n",
    "temps_missoula = [70, 71]\n",
    "temps_philly = [90, 91]\n",
    "temperatures = {'Missoula': temps_missoula,\n",
    "                'Philadelphia': temps_philly}\n",
    "pd.DataFrame(temperatures)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    0   1\n",
       "0  70  90\n",
       "1  71  91"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем датафрейм для списка объектов Series\n",
    "temps_at_time0 = pd.Series([70, 90])\n",
    "temps_at_time1 = pd.Series([71, 91])\n",
    "df = pd.DataFrame([temps_at_time0, temps_at_time1])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   Missoula  Philadelphia\n",
       "0       NaN           NaN\n",
       "1       NaN           NaN"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# попытаемся задать имена столбцов\n",
    "df = pd.DataFrame([temps_at_time0, temps_at_time1],\n",
    "                  columns=['Missoula', 'Philadelphia'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   Missoula  Philadelphia\n",
       "0        70            90\n",
       "1        71            91"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# задаем имена столбцов после создания датафрейма\n",
    "df = pd.DataFrame([temps_at_time0, temps_at_time1])\n",
    "df.columns = ['Missoula', 'Philadelphia']\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   Missoula  Philadelphia\n",
       "0        70            90\n",
       "1        71            91"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем датафрейм с помощью словаря,\n",
    "# состоящего из объектов Series\n",
    "temps_mso_series = pd.Series(temps_missoula)\n",
    "temps_phl_series = pd.Series(temps_philly)\n",
    "df = pd.DataFrame({'Missoula': temps_mso_series,\n",
    "                   'Philadelphia': temps_phl_series})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   Missoula  New York  Philadelphia\n",
       "0      70.0       NaN          90.0\n",
       "1      71.0      85.0          91.0\n",
       "2       NaN      87.0           NaN"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# выравнивание происходит при создании датафрейма\n",
    "temps_nyc_series = pd.Series([85, 87], index=[1, 2])\n",
    "df = pd.DataFrame({'Missoula': temps_mso_series,\n",
    "                   'Philadelphia': temps_phl_series,\n",
    "                   'New York': temps_nyc_series})\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Создание объекта DataFrame на основе CSV-файла"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# считываем данные и выводим первые пять строк\n",
    "# используем столбец Symbol в качестве индекса\n",
    "# и считываем только те столбцы, у которых\n",
    "# позиции 0, 2, 3, 7\n",
    "sp500 = pd.read_csv(\"C:/data/sp500.csv\", \n",
    "                    index_col='Symbol', \n",
    "                    usecols=[0, 2, 3, 7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                        Sector   Price  Book Value\n",
       "Symbol                                            \n",
       "MMM                Industrials  141.14      26.668\n",
       "ABT                Health Care   39.60      15.573\n",
       "ABBV               Health Care   53.95       2.954\n",
       "ACN     Information Technology   79.79       8.326\n",
       "ACE                 Financials  102.91      86.897"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# взглянем на первые 5 строк данных с помощью\n",
    "# метода .head()\n",
    "sp500.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "500"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# сколько строк данных? должно быть 500\n",
    "len(sp500)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(500, 3)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# какова форма?\n",
    "sp500.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1500"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# каков размер?\n",
    "sp500.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['MMM', 'ABT', 'ABBV', 'ACN', 'ACE', 'ACT', 'ADBE', 'AES', 'AET', 'AFL',\n",
       "       ...\n",
       "       'XEL', 'XRX', 'XLNX', 'XL', 'XYL', 'YHOO', 'YUM', 'ZMH', 'ZION', 'ZTS'],\n",
       "      dtype='object', name='Symbol', length=500)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# исследуем индекс\n",
    "sp500.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Sector', 'Price', 'Book Value'], dtype='object')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# получаем столбцы\n",
    "sp500.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Отбор столбцов в объекте DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Symbol\n",
       "MMM                Industrials\n",
       "ABT                Health Care\n",
       "ABBV               Health Care\n",
       "ACN     Information Technology\n",
       "ACE                 Financials\n",
       "Name: Sector, dtype: object"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# извлекаем столбец Sector\n",
    "sp500['Sector'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(sp500['Sector'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         Price  Book Value\n",
       "Symbol                    \n",
       "MMM     141.14      26.668\n",
       "ABT      39.60      15.573\n",
       "ABBV     53.95       2.954\n",
       "ACN      79.79       8.326\n",
       "ACE     102.91      86.897"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# извлекаем столбцы Price и Book Value\n",
    "sp500[['Price', 'Book Value']].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.frame.DataFrame"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# покажем, что результат является объектом DataFrame\n",
    "type(sp500[['Price', 'Book Value']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Symbol\n",
       "MMM     141.14\n",
       "ABT      39.60\n",
       "ABBV     53.95\n",
       "ACN      79.79\n",
       "ACE     102.91\n",
       "         ...  \n",
       "YHOO     35.02\n",
       "YUM      74.77\n",
       "ZMH     101.84\n",
       "ZION     28.43\n",
       "ZTS      30.53\n",
       "Name: Price, Length: 500, dtype: float64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# атрибутивный доступ к столбцу по имени\n",
    "sp500.Price"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Отбор строк в объекте DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Sector        Industrials\n",
       "Price              141.14\n",
       "Book Value         26.668\n",
       "Name: MMM, dtype: object"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# получаем строку с меткой индекса MMM,\n",
    "# которая возвращается в виде объекта Series\n",
    "sp500.loc['MMM']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                        Sector   Price  Book Value\n",
       "Symbol                                            \n",
       "MMM                Industrials  141.14      26.668\n",
       "MSFT    Information Technology   40.12      10.584"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# получаем строки MMM и MSFT\n",
    "# результатом будет объект DataFrame\n",
    "sp500.loc[['MMM', 'MSFT']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price  Book Value\n",
       "Symbol                                 \n",
       "MMM     Industrials  141.14      26.668\n",
       "ABBV    Health Care   53.95       2.954"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# получаем строки, имеющие позиции 0 и 2\n",
    "sp500.iloc[[0, 2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, 10)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# получаем позиции меток MMM и A в индексе\n",
    "i1 = sp500.index.get_loc('MMM')\n",
    "i2 = sp500.index.get_loc('A')\n",
    "(i1, i2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price  Book Value\n",
       "Symbol                                 \n",
       "MMM     Industrials  141.14      26.668\n",
       "A       Health Care   56.18      16.928"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# и извлекаем строки\n",
    "sp500.iloc[[i1, i2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Поиск скалярного значения по метке и позиции с помощью .at[] и .iat[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "141.13999999999999"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ищем скалярное значение по метке строки\n",
    "# и метке (имени) столбца\n",
    "sp500.at['MMM', 'Price']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "141.13999999999999"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ищем скалярное значение по позиции строки\n",
    "# и позиции столбца\n",
    "# извлекаем значение в строке 0, столбце 1\n",
    "sp500.iat[0, 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Создание среза датафрейма с помощью оператора []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                        Sector   Price  Book Value\n",
       "Symbol                                            \n",
       "MMM                Industrials  141.14      26.668\n",
       "ABT                Health Care   39.60      15.573\n",
       "ABBV               Health Care   53.95       2.954\n",
       "ACN     Information Technology   79.79       8.326\n",
       "ACE                 Financials  102.91      86.897"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# первые пять строк\n",
    "sp500[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                        Sector  Price  Book Value\n",
       "Symbol                                           \n",
       "ABT                Health Care  39.60      15.573\n",
       "ABBV               Health Care  53.95       2.954\n",
       "ACN     Information Technology  79.79       8.326"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# строки, начиная с метки ABT и заканчивая меткой ACN\n",
    "sp500['ABT':'ACN']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Логический отбор строк"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Symbol\n",
       "MMM     False\n",
       "ABT      True\n",
       "ABBV     True\n",
       "ACN      True\n",
       "ACE     False\n",
       "        ...  \n",
       "YHOO     True\n",
       "YUM      True\n",
       "ZMH     False\n",
       "ZION     True\n",
       "ZTS      True\n",
       "Name: Price, Length: 500, dtype: bool"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# какие строки имеют значения Price < 100?\n",
    "sp500.Price < 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                        Sector  Price  Book Value\n",
       "Symbol                                           \n",
       "ABT                Health Care  39.60      15.573\n",
       "ABBV               Health Care  53.95       2.954\n",
       "ACN     Information Technology  79.79       8.326\n",
       "ADBE    Information Technology  64.30      13.262\n",
       "AES                  Utilities  13.61       5.781\n",
       "...                        ...    ...         ...\n",
       "XYL                Industrials  38.42      12.127\n",
       "YHOO    Information Technology  35.02      12.768\n",
       "YUM     Consumer Discretionary  74.77       5.147\n",
       "ZION                Financials  28.43      30.191\n",
       "ZTS                Health Care  30.53       2.150\n",
       "\n",
       "[407 rows x 3 columns]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# теперь получим строки, в которых Price < 100\n",
    "sp500[sp500.Price < 100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Symbol\n",
       "HCBK    9.80\n",
       "HBAN    9.10\n",
       "SLM     8.82\n",
       "WIN     9.38\n",
       "Name: Price, dtype: float64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# извлекаем лишь те строки, в которых\n",
    "# значение Price < 10 и > 6\n",
    "r = sp500[(sp500.Price < 10) & \n",
    "          (sp500.Price > 6)] ['Price']\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         Price       Sector\n",
       "Symbol                     \n",
       "ACT     213.77  Health Care\n",
       "ALXN    162.30  Health Care\n",
       "AGN     166.92  Health Care\n",
       "AMGN    114.33  Health Care\n",
       "BCR     146.62  Health Care\n",
       "...        ...          ...\n",
       "REGN    297.77  Health Care\n",
       "TMO     115.74  Health Care\n",
       "WAT     100.54  Health Care\n",
       "WLP     108.82  Health Care\n",
       "ZMH     101.84  Health Care\n",
       "\n",
       "[19 rows x 2 columns]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# извлекаем строки, в которых переменная Sector\n",
    "# принимает значение Health Care, а переменная\n",
    "# Price больше или равна 100.00\n",
    "r = sp500[(sp500.Sector == 'Health Care') & \n",
    "          (sp500.Price > 100.00)] [['Price', 'Sector']]\n",
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Одновременный отбор строк и столбцов"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector  Price\n",
       "Symbol                    \n",
       "ABT     Health Care  39.60\n",
       "ZTS     Health Care  30.53"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# отбираем строки с метками индекса ABT и ZTS\n",
    "# для столбцов Sector и Price\n",
    "sp500.loc[['ABT', 'ZTS']][['Sector', 'Price']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
