{
 "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', 7)\n",
    "pd.set_option('display.max_rows', 10)\n",
    "pd.set_option('display.width', 60)\n",
    "\n",
    "# считываем данные в DataFrame, используя в качестве\n",
    "# индекса столбец Symbol и записывая только те \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": "markdown",
   "metadata": {},
   "source": [
    "# Переименование столбцов"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price  BookValue\n",
       "Symbol                                \n",
       "MMM     Industrials  141.14     26.668\n",
       "ABT     Health Care   39.60     15.573"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# переименовываем столбец Book Value так, чтобы удалить пробел\n",
    "# программный код возращает копию датафрейма с переименованным \n",
    "# столбцом\n",
    "newSP500 = sp500.rename(columns=\n",
    "                        {'Book Value': 'BookValue'})\n",
    "# печатаем первые 2 строки\n",
    "newSP500[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Sector', 'Price', 'Book Value'], dtype='object')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# проверяем, не изменились ли имена столбцов\n",
    "# в исходном датафрейме\n",
    "sp500.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Sector', 'Price', 'BookValue'], dtype='object')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# этот программный код переименовывает\n",
    "# столбец на месте\n",
    "sp500.rename(columns=                  \n",
    "             {'Book Value': 'BookValue'},                   \n",
    "             inplace=True)\n",
    "# смотрим, изменилось ли имя столбца\n",
    "sp500.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Symbol\n",
       "MMM     26.668\n",
       "ABT     15.573\n",
       "ABBV     2.954\n",
       "ACN      8.326\n",
       "ACE     86.897\n",
       "Name: BookValue, dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# и теперь мы можем воспользоваться свойством .BookValue\n",
    "sp500.BookValue[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Добавление новых столбцов с помощью оператора [] и метода .insert()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price  BookValue  RoundedPrice\n",
       "Symbol                                              \n",
       "MMM     Industrials  141.14     26.668         141.0\n",
       "ABT     Health Care   39.60     15.573          40.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем копию, чтобы исходные данные остались в неизменном виде\n",
    "sp500_copy = sp500.copy()\n",
    "# добавляем столбец\n",
    "sp500_copy['RoundedPrice'] = sp500.Price.round()\n",
    "sp500_copy[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector  RoundedPrice   Price  BookValue\n",
       "Symbol                                              \n",
       "MMM     Industrials         141.0  141.14     26.668\n",
       "ABT     Health Care          40.0   39.60     15.573"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем копию, чтобы исходные данные остались в неизменном виде\n",
    "copy = sp500.copy()\n",
    "# вставляем столбец RoundedPrice в качестве \n",
    "# второго столбца датафрейма\n",
    "copy.insert(1, 'RoundedPrice', sp500.Price.round())\n",
    "copy[:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Добавление столбцов за счет расширения датафрейма"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price  BookValue  PER\n",
       "Symbol                                     \n",
       "MMM     Industrials  141.14     26.668    0\n",
       "ABT     Health Care   39.60     15.573    0\n",
       "ABBV    Health Care   53.95      2.954    0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем копию поднабора/среза\n",
    "ss = sp500[:3].copy()\n",
    "# добавляем столбец с нулевыми значениями\n",
    "ss.loc[:,'PER'] = 0\n",
    "# смотрим результаты\n",
    "ss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price  BookValue       PER\n",
       "Symbol                                          \n",
       "MMM     Industrials  141.14     26.668  0.469112\n",
       "ABT     Health Care   39.60     15.573 -0.282863\n",
       "ABBV    Health Care   53.95      2.954 -1.509059"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем копию поднабора/среза\n",
    "ss = sp500[:3].copy()\n",
    "# добавляем новый столбец со случайно \n",
    "# сгенерированными значениями\n",
    "np.random.seed(123456)\n",
    "ss.loc[:,'PER'] = pd.Series(np.random.normal(size=3), index=ss.index)\n",
    "# смотрим результаты\n",
    "ss"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Добавление столбцов с помощью конкатенации"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                        Sector   Price  BookValue  \\\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   \n",
       "\n",
       "        RoundedPrice  \n",
       "Symbol                \n",
       "MMM            141.0  \n",
       "ABT             40.0  \n",
       "ABBV            54.0  \n",
       "ACN             80.0  \n",
       "ACE            103.0  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем объект DataFrame с единственным \n",
    "# столбцом RoundedPrice\n",
    "rounded_price = pd.DataFrame({'RoundedPrice':    \n",
    "                              sp500.Price.round()})\n",
    "# конкатенируем по оси столбцов\n",
    "concatenated = pd.concat([sp500, rounded_price], axis=1)\n",
    "concatenated[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        Price\n",
       "Symbol       \n",
       "MMM     141.0\n",
       "ABT      40.0\n",
       "ABBV     54.0\n",
       "ACN      80.0\n",
       "ACE     103.0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем объект DataFrame с единственным \n",
    "# столбцом Price\n",
    "rounded_price = pd.DataFrame({'Price': sp500.Price.round()})\n",
    "rounded_price[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                        Sector   Price  BookValue  Price\n",
       "Symbol                                                  \n",
       "MMM                Industrials  141.14     26.668  141.0\n",
       "ABT                Health Care   39.60     15.573   40.0\n",
       "ABBV               Health Care   53.95      2.954   54.0\n",
       "ACN     Information Technology   79.79      8.326   80.0\n",
       "ACE                 Financials  102.91     86.897  103.0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# в результате получаем два столбца Price\n",
    "dups = pd.concat([sp500, rounded_price], axis=1)\n",
    "dups[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         Price  Price\n",
       "Symbol               \n",
       "MMM     141.14  141.0\n",
       "ABT      39.60   40.0\n",
       "ABBV     53.95   54.0\n",
       "ACN      79.79   80.0\n",
       "ACE     102.91  103.0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# извлекаем оба столбца Price\n",
    "dups.Price[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Переупорядочивание столбцов"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        BookValue   Price                  Sector\n",
       "Symbol                                           \n",
       "MMM        26.668  141.14             Industrials\n",
       "ABT        15.573   39.60             Health Care\n",
       "ABBV        2.954   53.95             Health Care\n",
       "ACN         8.326   79.79  Information Technology\n",
       "ACE        86.897  102.91              Financials"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# возвращаем новый объект DataFrame со столбцами,\n",
    "# расположенными в обратном порядке\n",
    "reversed_column_names = sp500.columns[::-1]\n",
    "sp500[reversed_column_names][:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Замена содержимого столбца"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                        Sector  Price  BookValue\n",
       "Symbol                                          \n",
       "MMM                Industrials  141.0     26.668\n",
       "ABT                Health Care   40.0     15.573\n",
       "ABBV               Health Care   54.0      2.954\n",
       "ACN     Information Technology   80.0      8.326\n",
       "ACE                 Financials  103.0     86.897"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# операция выполняется на месте, поэтому создадим копию\n",
    "copy = sp500.copy()\n",
    "# заменяем данные в столбце Price новыми значениями\n",
    "# вместо добавления нового столбца\n",
    "copy.Price = rounded_price.Price\n",
    "copy[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                        Sector  Price  BookValue\n",
       "Symbol                                          \n",
       "MMM                Industrials  141.0     26.668\n",
       "ABT                Health Care   40.0     15.573\n",
       "ABBV               Health Care   54.0      2.954\n",
       "ACN     Information Technology   80.0      8.326\n",
       "ACE                 Financials  103.0     86.897"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# операция выполняется на месте, поэтому создадим копию\n",
    "copy = sp500.copy()\n",
    "# заменяем данные в столбце Price округленными значениями\n",
    "copy.loc[:,'Price'] = rounded_price.Price\n",
    "copy[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Удаление столбцов"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price\n",
       "Symbol                     \n",
       "MMM     Industrials  141.14\n",
       "ABT     Health Care   39.60"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# пример использования del для удаления столбца\n",
    "# делаем копию, потому что операция выполняется на месте\n",
    "copy = sp500.copy()\n",
    "del copy['BookValue']\n",
    "copy[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         Price  BookValue\n",
       "Symbol                   \n",
       "MMM     141.14     26.668\n",
       "ABT      39.60     15.573"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# пример использования pop для удаления столбца из датафрейма\n",
    "# делаем копию, потому что операция выполняется на месте\n",
    "copy = sp500.copy()\n",
    "# эта строка удалит столбец Sector и возвратит его как серию\n",
    "popped = copy.pop('Sector')\n",
    "# столбец Sector удален на месте\n",
    "copy[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "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": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# и у нас есть столбец Sector, полученный\n",
    "# в результате применения pop\n",
    "popped[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         Price  BookValue\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": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# пример использования drop для удаления столбца из датафрейма\n",
    "# делаем копию\n",
    "copy = sp500.copy()\n",
    "# эта строка вернет новый датафрейм с удаленным столбцом 'Sector’\n",
    "# копия датафрейма не изменится\n",
    "afterdrop = copy.drop(['Sector'], axis = 1)\n",
    "afterdrop[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Присоединение строк из других объектов DataFrame с помощью метода .append()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price  BookValue\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",
       "A       Health Care   56.18     16.928\n",
       "GAS       Utilities   52.98     32.462\n",
       "ABBV    Health Care   53.95      2.954"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# копируем первые три строки датафрейма sp500\n",
    "df1 = sp500.iloc[0:3].copy()\n",
    "# копируем строки в позициях 10, 11 и 2\n",
    "df2 = sp500.iloc[[10, 11, 2]]\n",
    "# присоединяем к датафрейму df1 датафрейм df2\n",
    "appended = df1.append(df2)\n",
    "# в результате к строкам первого датафрейма\n",
    "# будут присоединены строки второго датафрейма\n",
    "appended"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        PER\n",
       "Symbol     \n",
       "MMM     0.0\n",
       "ABT     0.0\n",
       "ABBV    0.0"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# датафрейм df3 использует индекс датафрейма df1 \n",
    "# и у него один столбец PER\n",
    "# кроме того, это еще и хороший пример \n",
    "# использования скалярного значения\n",
    "# для инициализации нескольких строк\n",
    "df3 = pd.DataFrame(0.0, \n",
    "                   index=df1.index,\n",
    "                   columns=['PER'])\n",
    "df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        BookValue  PER   Price       Sector\n",
       "Symbol                                     \n",
       "MMM        26.668  NaN  141.14  Industrials\n",
       "ABT        15.573  NaN   39.60  Health Care\n",
       "ABBV        2.954  NaN   53.95  Health Care\n",
       "MMM           NaN  0.0     NaN          NaN\n",
       "ABT           NaN  0.0     NaN          NaN\n",
       "ABBV          NaN  0.0     NaN          NaN"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# присоединяем к датафрейму df1 датафрейм df3\n",
    "# каждый датафрейм содержит по три строки, таким \n",
    "# образом в итоге получим шесть строк\n",
    "# df1 не имеет столбца PER, таким образом для строк df1, вошедших \n",
    "# в итоговый датафрейм, в столбце PER будут получены значения NaN\n",
    "# df3 не имеет столбцов BookValue, Price и Sector, таким образом для \n",
    "# строк df3, вошедших в итоговый датафрейм, в столбцах BookValue, Price \n",
    "# и Sector также будут получены значения NaN\n",
    "df1.append(df3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   BookValue  PER   Price       Sector\n",
       "0     26.668  NaN  141.14  Industrials\n",
       "1     15.573  NaN   39.60  Health Care\n",
       "2      2.954  NaN   53.95  Health Care\n",
       "3        NaN  0.0     NaN          NaN\n",
       "4        NaN  0.0     NaN          NaN\n",
       "5        NaN  0.0     NaN          NaN"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# игнорируем метки индекса, создаем индекс по умолчанию\n",
    "df1.append(df3, ignore_index=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Конкатенация строк"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price  BookValue\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",
       "A       Health Care   56.18     16.928\n",
       "GAS       Utilities   52.98     32.462\n",
       "ABBV    Health Care   53.95      2.954"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# копируем первые три строки датафрейма sp500\n",
    "df1 = sp500.iloc[0:3].copy()\n",
    "# копируем строки в позициях 10, 11 и 2\n",
    "df2 = sp500.iloc[[10, 11, 2]]\n",
    "# передаем их в виде списка\n",
    "pd.concat([df1, df2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector  Price  BookValue  Foo\n",
       "Symbol                                    \n",
       "A       Health Care  56.18     16.928    0\n",
       "GAS       Utilities  52.98     32.462    0\n",
       "ABBV    Health Care  53.95      2.954    0"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# копируем df2\n",
    "df2_2 = df2.copy()\n",
    "# добавляем в df2_2 столбец, которого нет в df1\n",
    "df2_2.insert(3, 'Foo', pd.Series(0, index=df2.index))\n",
    "# смотрим df2_2\n",
    "df2_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        BookValue  Foo   Price       Sector\n",
       "Symbol                                     \n",
       "MMM        26.668  NaN  141.14  Industrials\n",
       "ABT        15.573  NaN   39.60  Health Care\n",
       "ABBV        2.954  NaN   53.95  Health Care\n",
       "A          16.928  0.0   56.18  Health Care\n",
       "GAS        32.462  0.0   52.98    Utilities\n",
       "ABBV        2.954  0.0   53.95  Health Care"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# теперь конкатенируем\n",
    "pd.concat([df1, df2_2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            BookValue  Foo   Price       Sector\n",
       "    Symbol                                     \n",
       "df1 MMM        26.668  NaN  141.14  Industrials\n",
       "    ABT        15.573  NaN   39.60  Health Care\n",
       "    ABBV        2.954  NaN   53.95  Health Care\n",
       "df2 A          16.928  0.0   56.18  Health Care\n",
       "    GAS        32.462  0.0   52.98    Utilities\n",
       "    ABBV        2.954  0.0   53.95  Health Care"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# задаем ключи\n",
    "r = pd.concat([df1, df2_2], keys=['df1', 'df2'])\n",
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Добавление и замена строк за счет расширения датафрейма"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price  BookValue\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",
       "FOO      the sector  100.00    110.000"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем срез датафрейма sp500 и копируем его \n",
    "ss = sp500[:3].copy()\n",
    "# создаем новую строку с меткой индекса FOO\n",
    "# и присваиваем столбцам некоторые значения \n",
    "# с помощью списка\n",
    "ss.loc['FOO'] = ['the sector', 100, 110]\n",
    "ss"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Удаление строк с помощью метода .drop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                        Sector   Price  BookValue\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": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# получаем копию первых 5 строк датафрейма sp500\n",
    "ss = sp500[:5]\n",
    "ss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price  BookValue\n",
       "Symbol                                \n",
       "MMM     Industrials  141.14     26.668\n",
       "ABBV    Health Care   53.95      2.954\n",
       "ACE      Financials  102.91     86.897"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# удаляем строки с метками ABT и ACN\n",
    "afterdrop = ss.drop(['ABT', 'ACN'])\n",
    "afterdrop[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Удаление строк с помощью логического отбора"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(500, 10)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# определяем строки, в которых Price > 300\n",
    "selection = sp500.Price > 300\n",
    "# выводим информацию о количестве строк и \n",
    "# количестве строк, которые будут удалены\n",
    "(len(selection), selection.sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                        Sector   Price  BookValue\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\n",
       "...                        ...     ...        ...\n",
       "YHOO    Information Technology   35.02     12.768\n",
       "YUM     Consumer Discretionary   74.77      5.147\n",
       "ZMH                Health Care  101.84     37.181\n",
       "ZION                Financials   28.43     30.191\n",
       "ZTS                Health Care   30.53      2.150\n",
       "\n",
       "[490 rows x 3 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# для отбора применим побитовое отрицание\n",
    "# к выражению selection\n",
    "price_less_than_300 = sp500[~selection]\n",
    "price_less_than_300"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Удаление строк с помощью среза"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price  BookValue\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"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# отбираем лишь первые три строки\n",
    "only_first_three = sp500[:3]\n",
    "only_first_three"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             Sector   Price  BookValue\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"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# отбираем лишь первые три строки,\n",
    "# но теперь копируем их\n",
    "only_first_three = sp500[:3].copy()\n",
    "only_first_three"
   ]
  }
 ],
 "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
}
