{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Основы программирования в Python\n", "\n", "*Алла Тамбовцева, НИУ ВШЭ*\n", "\n", "## Датафреймы `pandas`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Продолжим работать с таблицами в Python. Для начала импортируем саму библиотеку." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Загрузим таблицу:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv(\"http://math-info.hse.ru/f/2017-18/py-prog/scores2.csv\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "И посмотрим на её структуру более внимательно. Выберем первый столбец с *id*:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 М141БПЛТЛ024\n", "1 М141БПЛТЛ031\n", "2 М141БПЛТЛ075\n", "3 М141БПЛТЛ017\n", "4 М141БПЛТЛ069\n", "5 М141БПЛТЛ072\n", "6 М141БПЛТЛ020\n", "7 М141БПЛТЛ026\n", "8 М141БПЛТЛ073\n", "9 М141БПЛТЛ078\n", "10 М141БПЛТЛ060\n", "11 М141БПЛТЛ040\n", "12 М141БПЛТЛ065\n", "13 М141БПЛТЛ053\n", "14 М141БПЛТЛ015\n", "15 М141БПЛТЛ021\n", "16 М141БПЛТЛ018\n", "17 М141БПЛТЛ039\n", "18 М141БПЛТЛ036\n", "19 М141БПЛТЛ049\n", "20 06114043\n", "21 М141БПЛТЛ048\n", "22 М141БПЛТЛ034\n", "23 М141БПЛТЛ045\n", "24 М141БПЛТЛ033\n", "25 М141БПЛТЛ083\n", "26 М141БПЛТЛ008\n", "27 М141БПЛТЛ001\n", "28 М141БПЛТЛ038\n", "29 М141БПЛТЛ052\n", "30 М141БПЛТЛ011\n", "31 М141БПЛТЛ004\n", "32 М141БПЛТЛ010\n", "33 М141БПЛТЛ071\n", "34 М141БПЛТЛ035\n", "35 М141БПЛТЛ030\n", "36 М141БПЛТЛ070\n", "37 М141БПЛТЛ051\n", "38 М141БПЛТЛ046\n", "39 М141БПЛТЛ047\n", "40 М141БПЛТЛ063\n", "41 М141БПЛТЛ029\n", "42 М141БПЛТЛ064\n", "43 М141БПЛТЛ076\n", "44 М141БПЛТЛ062\n", "45 М141БПЛТЛ074\n", "46 130232038\n", "47 М141БПЛТЛ023\n", "48 М141БПЛТЛ054\n", "49 М141БПЛТЛ012\n", "50 М141БПЛТЛ006\n", "51 М141БПЛТЛ055\n", "52 М141БПЛТЛ007\n", "53 М141БПЛТЛ050\n", "54 М141БПЛТЛ066\n", "55 М141БПЛТЛ043\n", "56 М141БПЛТЛ084\n", "57 М141БПЛТЛ005\n", "58 М141БПЛТЛ044\n", "59 13051038\n", "Name: id, dtype: object" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['id']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Столбец датафрейма `df` имеет особый тип *Series*. Внешне *Series* отличается от обычного списка значений, потому что, во-первых, при вызове столбца на экран выводятся не только сами элементы, но их номер (номер строки), а во-вторых, на экран выводится строка с названием столбца (`Name: id`) и его тип (`dtype: object`, текстовый). Первая особенность роднит *Series* со словарями: он представляет собой пары *ключ-значение*, то есть *номер-значение*. Вторая особенность роднит *Series* с массивами `numpy`: элементы обычно должны быть одного типа.\n", "\n", "Библиотеку `numpy` мы еще не обсуждали, но обязательно обсудим позже, так как во многих задачах использовать массивы `numpy` гораздо удобнее, чем списки. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Можно вывести первые или последние строки таблицы, используя методы `.head()` и `.tail()`." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idcatpsmstatsoceconengpolthmstat2phistlawphilpolsocptheopregcomppgamewpolmale
0М141БПЛТЛ024798898108.07997.088.06101
1М141БПЛТЛ03181010101010109.0910109.088.09101
2М141БПЛТЛ0759991091098.091099.088.0791
3М141БПЛТЛ017998899106.09998.088.0890
4М141БПЛТЛ06910101010101098.081097.065.08101
\n", "
" ], "text/plain": [ " id catps mstat soc econ eng polth mstat2 phist law \\\n", "0 М141БПЛТЛ024 7 9 8 8 9 8 10 8.0 7 \n", "1 М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 \n", "2 М141БПЛТЛ075 9 9 9 10 9 10 9 8.0 9 \n", "3 М141БПЛТЛ017 9 9 8 8 9 9 10 6.0 9 \n", "4 М141БПЛТЛ069 10 10 10 10 10 10 9 8.0 8 \n", "\n", " phil polsoc ptheo preg compp game wpol male \n", "0 9 9 7.0 8 8.0 6 10 1 \n", "1 10 10 9.0 8 8.0 9 10 1 \n", "2 10 9 9.0 8 8.0 7 9 1 \n", "3 9 9 8.0 8 8.0 8 9 0 \n", "4 10 9 7.0 6 5.0 8 10 1 " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idcatpsmstatsoceconengpolthmstat2phistlawphilpolsocptheopregcomppgamewpolmale
55М141БПЛТЛ04355658565.06454.05NaN460
56М141БПЛТЛ0846784855NaN8444.044.0671
57М141БПЛТЛ00557557474.05455.044.0481
58М141БПЛТЛ04445746445.04444.06NaN551
591305103854449555.0544NaN74.0441
\n", "
" ], "text/plain": [ " id catps mstat soc econ eng polth mstat2 phist law \\\n", "55 М141БПЛТЛ043 5 5 6 5 8 5 6 5.0 6 \n", "56 М141БПЛТЛ084 6 7 8 4 8 5 5 NaN 8 \n", "57 М141БПЛТЛ005 5 7 5 5 7 4 7 4.0 5 \n", "58 М141БПЛТЛ044 4 5 7 4 6 4 4 5.0 4 \n", "59 13051038 5 4 4 4 9 5 5 5.0 5 \n", "\n", " phil polsoc ptheo preg compp game wpol male \n", "55 4 5 4.0 5 NaN 4 6 0 \n", "56 4 4 4.0 4 4.0 6 7 1 \n", "57 4 5 5.0 4 4.0 4 8 1 \n", "58 4 4 4.0 6 NaN 5 5 1 \n", "59 4 4 NaN 7 4.0 4 4 1 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.tail()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Внимание:** это просто первые и последние строки таблицы «как есть». Никакой сортировки не происходит! \n", "\n", "По умолчанию эти методы выводят пять строк, но при желании это легко изменить. Достаточно в скобках указать желаемое число строк." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idcatpsmstatsoceconengpolthmstat2phistlawphilpolsocptheopregcomppgamewpolmale
0М141БПЛТЛ024798898108.07997.088.06101
1М141БПЛТЛ03181010101010109.0910109.088.09101
2М141БПЛТЛ0759991091098.091099.088.0791
3М141БПЛТЛ017998899106.09998.088.0890
4М141БПЛТЛ06910101010101098.081097.065.08101
5М141БПЛТЛ0721098109898.081097.088.0990
6М141БПЛТЛ020877691088.07797.086.0891
7М141БПЛТЛ0267108710798.08888.087.0780
8М141БПЛТЛ07379889898.08997.076.01091
9М141БПЛТЛ078669561076.08696.088.0670
\n", "
" ], "text/plain": [ " id catps mstat soc econ eng polth mstat2 phist law \\\n", "0 М141БПЛТЛ024 7 9 8 8 9 8 10 8.0 7 \n", "1 М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 \n", "2 М141БПЛТЛ075 9 9 9 10 9 10 9 8.0 9 \n", "3 М141БПЛТЛ017 9 9 8 8 9 9 10 6.0 9 \n", "4 М141БПЛТЛ069 10 10 10 10 10 10 9 8.0 8 \n", "5 М141БПЛТЛ072 10 9 8 10 9 8 9 8.0 8 \n", "6 М141БПЛТЛ020 8 7 7 6 9 10 8 8.0 7 \n", "7 М141БПЛТЛ026 7 10 8 7 10 7 9 8.0 8 \n", "8 М141БПЛТЛ073 7 9 8 8 9 8 9 8.0 8 \n", "9 М141БПЛТЛ078 6 6 9 5 6 10 7 6.0 8 \n", "\n", " phil polsoc ptheo preg compp game wpol male \n", "0 9 9 7.0 8 8.0 6 10 1 \n", "1 10 10 9.0 8 8.0 9 10 1 \n", "2 10 9 9.0 8 8.0 7 9 1 \n", "3 9 9 8.0 8 8.0 8 9 0 \n", "4 10 9 7.0 6 5.0 8 10 1 \n", "5 10 9 7.0 8 8.0 9 9 0 \n", "6 7 9 7.0 8 6.0 8 9 1 \n", "7 8 8 8.0 8 7.0 7 8 0 \n", "8 9 9 7.0 7 6.0 10 9 1 \n", "9 6 9 6.0 8 8.0 6 7 0 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head(10) # первые 10 строк" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Давайте кое-что подкорректруем. Сделаем так, чтобы строки в таблице назывались в соответствии с `id`. Другими словами, сделаем так, чтобы первый столбец считался индексом строки:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv(\"http://math-info.hse.ru/f/2017-18/py-prog/scores2.csv\", index_col = 0)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
catpsmstatsoceconengpolthmstat2phistlawphilpolsocptheopregcomppgamewpolmale
id
М141БПЛТЛ024798898108.07997.088.06101
М141БПЛТЛ03181010101010109.0910109.088.09101
М141БПЛТЛ0759991091098.091099.088.0791
М141БПЛТЛ017998899106.09998.088.0890
М141БПЛТЛ06910101010101098.081097.065.08101
\n", "
" ], "text/plain": [ " catps mstat soc econ eng polth mstat2 phist law phil \\\n", "id \n", "М141БПЛТЛ024 7 9 8 8 9 8 10 8.0 7 9 \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 10 \n", "М141БПЛТЛ075 9 9 9 10 9 10 9 8.0 9 10 \n", "М141БПЛТЛ017 9 9 8 8 9 9 10 6.0 9 9 \n", "М141БПЛТЛ069 10 10 10 10 10 10 9 8.0 8 10 \n", "\n", " polsoc ptheo preg compp game wpol male \n", "id \n", "М141БПЛТЛ024 9 7.0 8 8.0 6 10 1 \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 \n", "М141БПЛТЛ075 9 9.0 8 8.0 7 9 1 \n", "М141БПЛТЛ017 9 8.0 8 8.0 8 9 0 \n", "М141БПЛТЛ069 9 7.0 6 5.0 8 10 1 " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head() # теперь так" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Иногда такой подход может быть полезен. Представьте, что все переменные в таблице, кроме *id*, измерены в количественной шкале, и мы планируем реализовать на них статистический метод, который работает исключительно с числовыми данными. Если мы просто выкинем столбец с *id*, мы потеряем информацию о наблюдении, если мы его оставим, нам придется собирать в отдельную таблицу показатели, к которым будем применять метод, так как сохраненный в исходной таблице текст будет мешать. Если же мы назовем строки в соответствии с *id*, мы убьем сразу двух зайцев: избавимся от столбца с текстом и не потеряем информацию о наблюдении (код, имя респондента, название страны и прочее)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Когда таблица большая, увидеть все столбцы разом не получится. Поэтому полезно знать, как получить список названий столбцов." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['catps', 'mstat', 'soc', 'econ', 'eng', 'polth', 'mstat2', 'phist',\n", " 'law', 'phil', 'polsoc', 'ptheo', 'preg', 'compp', 'game', 'wpol',\n", " 'male'],\n", " dtype='object')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.columns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Обратите внимание: полученный объект не является обычным списком:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pandas.core.indexes.base.Index" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(df.columns) # это Index из pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Чтобы получить список названий, достаточно сконвертировать тип с помощью привычного `list()`: " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['catps', 'mstat', 'soc', 'econ', 'eng', 'polth', 'mstat2', 'phist', 'law', 'phil', 'polsoc', 'ptheo', 'preg', 'compp', 'game', 'wpol', 'male']\n" ] } ], "source": [ "c = list(df.columns)\n", "print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Аналогичная история со строками: " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['М141БПЛТЛ024', 'М141БПЛТЛ031', 'М141БПЛТЛ075', 'М141БПЛТЛ017',\n", " 'М141БПЛТЛ069', 'М141БПЛТЛ072', 'М141БПЛТЛ020', 'М141БПЛТЛ026',\n", " 'М141БПЛТЛ073', 'М141БПЛТЛ078', 'М141БПЛТЛ060', 'М141БПЛТЛ040',\n", " 'М141БПЛТЛ065', 'М141БПЛТЛ053', 'М141БПЛТЛ015', 'М141БПЛТЛ021',\n", " 'М141БПЛТЛ018', 'М141БПЛТЛ039', 'М141БПЛТЛ036', 'М141БПЛТЛ049',\n", " '06114043', 'М141БПЛТЛ048', 'М141БПЛТЛ034', 'М141БПЛТЛ045',\n", " 'М141БПЛТЛ033', 'М141БПЛТЛ083', 'М141БПЛТЛ008', 'М141БПЛТЛ001',\n", " 'М141БПЛТЛ038', 'М141БПЛТЛ052', 'М141БПЛТЛ011', 'М141БПЛТЛ004',\n", " 'М141БПЛТЛ010', 'М141БПЛТЛ071', 'М141БПЛТЛ035', 'М141БПЛТЛ030',\n", " 'М141БПЛТЛ070', 'М141БПЛТЛ051', 'М141БПЛТЛ046', 'М141БПЛТЛ047',\n", " 'М141БПЛТЛ063', 'М141БПЛТЛ029', 'М141БПЛТЛ064', 'М141БПЛТЛ076',\n", " 'М141БПЛТЛ062', 'М141БПЛТЛ074', '130232038', 'М141БПЛТЛ023',\n", " 'М141БПЛТЛ054', 'М141БПЛТЛ012', 'М141БПЛТЛ006', 'М141БПЛТЛ055',\n", " 'М141БПЛТЛ007', 'М141БПЛТЛ050', 'М141БПЛТЛ066', 'М141БПЛТЛ043',\n", " 'М141БПЛТЛ084', 'М141БПЛТЛ005', 'М141БПЛТЛ044', '13051038'],\n", " dtype='object', name='id')" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.index" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Переименование столбцов и строк" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Раз названия столбцов можно вывести в виде некоторого перечня, то этот перечень можно редактировать. Посмотрим на названия столбцов ещё раз." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['catps', 'mstat', 'soc', 'econ', 'eng', 'polth', 'mstat2', 'phist',\n", " 'law', 'phil', 'polsoc', 'ptheo', 'preg', 'compp', 'game', 'wpol',\n", " 'male'],\n", " dtype='object')" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.columns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Давайте переименуем переменную `catps` в `cps`, чтобы думать о политической науке, а не о котах :) Для этого сохраним названия в список `my_cols` и изменим в списке первый элемент:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "my_cols = list(df.columns)\n", "my_cols[0] = \"cps\"" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['cps', 'mstat', 'soc', 'econ', 'eng', 'polth', 'mstat2', 'phist', 'law',\n", " 'phil', 'polsoc', 'ptheo', 'preg', 'compp', 'game', 'wpol', 'male'],\n", " dtype='object')" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.columns = my_cols # сохраним изменения в самой базе df\n", "df.columns # все обновилось!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Обратите внимание: для того, чтобы изменить одно или несколько названий, совсем необязательно создавать новый список «с нуля». Достаточно определить индексы нужных элементов и поправить только необходимые названия. \n", "\n", "Точно так же можно было поступить со строками. Но давайте лучше попробуем внести изменения в названия всех столбцов: сделаем так, чтобы все названия столбцов начинались с большой буквы. Для начала напишем функцию, которая принимает на вход датафрейм, обращается к его столбцам и переименовывает их, делая первую букву заглавной." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def rename_cols(df):\n", " oldnames = list(df.columns) # список старых названий \n", " newnames = [i.capitalize() for i in oldnames] # список новых названий\n", " df.columns = newnames # сохранение изменений\n", " return df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Теперь применим нашу функцию `rename_cols()` к базе `df`:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMale
id
М141БПЛТЛ024798898108.07997.088.06101
М141БПЛТЛ03181010101010109.0910109.088.09101
М141БПЛТЛ0759991091098.091099.088.0791
М141БПЛТЛ017998899106.09998.088.0890
М141БПЛТЛ06910101010101098.081097.065.08101
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ024 7 9 8 8 9 8 10 8.0 7 9 \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 10 \n", "М141БПЛТЛ075 9 9 9 10 9 10 9 8.0 9 10 \n", "М141БПЛТЛ017 9 9 8 8 9 9 10 6.0 9 9 \n", "М141БПЛТЛ069 10 10 10 10 10 10 9 8.0 8 10 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ024 9 7.0 8 8.0 6 10 1 \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 \n", "М141БПЛТЛ075 9 9.0 8 8.0 7 9 1 \n", "М141БПЛТЛ017 9 8.0 8 8.0 8 9 0 \n", "М141БПЛТЛ069 9 7.0 6 5.0 8 10 1 " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2 = rename_cols(df)\n", "df2.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Кажется, что таким способом мы сохранили изменения в новой базе `df2`, а старую базу `df` не тронули. Однако, если мы посмотрим на базу `df`, мы увидим, что она тоже изменилась!" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMale
id
М141БПЛТЛ024798898108.07997.088.06101
М141БПЛТЛ03181010101010109.0910109.088.09101
М141БПЛТЛ0759991091098.091099.088.0791
М141БПЛТЛ017998899106.09998.088.0890
М141БПЛТЛ06910101010101098.081097.065.08101
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ024 7 9 8 8 9 8 10 8.0 7 9 \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 10 \n", "М141БПЛТЛ075 9 9 9 10 9 10 9 8.0 9 10 \n", "М141БПЛТЛ017 9 9 8 8 9 9 10 6.0 9 9 \n", "М141БПЛТЛ069 10 10 10 10 10 10 9 8.0 8 10 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ024 9 7.0 8 8.0 6 10 1 \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 \n", "М141БПЛТЛ075 9 9.0 8 8.0 7 9 1 \n", "М141БПЛТЛ017 9 8.0 8 8.0 8 9 0 \n", "М141БПЛТЛ069 9 7.0 6 5.0 8 10 1 " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Почему это произошло? Потому что датафреймы являются изменяемой структурой данных (да-да, как списки). Поэтому, применяя методы к объекту типа `DataFrame`, мы меняем исходный датафрейм, и к этому надо быть готовым. Если вы не планируете вносить изменения в исходную базу, имеет смысл сделать её копию и работать с ней. Например, вот так:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "# метод copy\n", "df_new = df.copy()\n", "\n", "# вносим изменения в df_new - переименовываем один столбец\n", "new_cols = list(df_new.columns)\n", "new_cols[1] = \"Matstat\"\n", "df_new.columns = new_cols" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ024 7 9 8 8 9 8 10 8.0 7 9 \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 10 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ024 9 7.0 8 8.0 6 10 1 \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 \n", "\n", "\n", " Cps Matstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ024 7 9 8 8 9 8 10 8.0 7 9 \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 10 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ024 9 7.0 8 8.0 6 10 1 \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 \n" ] } ], "source": [ "# сравниваем\n", "\n", "print(df.head(2))\n", "print(\"\\n\") # для пустой строчки между df и df_new\n", "print(df_new.head(2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Обратите внимание:** создать копию обычным присваиванием не получится, код вида `df_new = df` создаст новую ссылку на датафрейм, но не новый датафрейм. Поэтому при изменении `df_new` база `df` также изменится (вспомните историю о коварстве списков)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Выбор столбцов и строк таблицы" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Выбор столбцов по названию**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Часто удобнее всего выбирать столбец по названию. Для этого достаточно указать название столбца в квадратных скобках (и обязательно в кавычках, так как название является строкой):" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "id\n", "М141БПЛТЛ024 9\n", "М141БПЛТЛ031 10\n", "М141БПЛТЛ075 9\n", "М141БПЛТЛ017 9\n", "М141БПЛТЛ069 10\n", "М141БПЛТЛ072 9\n", "М141БПЛТЛ020 7\n", "М141БПЛТЛ026 10\n", "М141БПЛТЛ073 9\n", "М141БПЛТЛ078 6\n", "М141БПЛТЛ060 8\n", "М141БПЛТЛ040 9\n", "М141БПЛТЛ065 9\n", "М141БПЛТЛ053 7\n", "М141БПЛТЛ015 9\n", "М141БПЛТЛ021 9\n", "М141БПЛТЛ018 7\n", "М141БПЛТЛ039 8\n", "М141БПЛТЛ036 10\n", "М141БПЛТЛ049 7\n", "06114043 8\n", "М141БПЛТЛ048 6\n", "М141БПЛТЛ034 9\n", "М141БПЛТЛ045 8\n", "М141БПЛТЛ033 9\n", "М141БПЛТЛ083 5\n", "М141БПЛТЛ008 8\n", "М141БПЛТЛ001 7\n", "М141БПЛТЛ038 9\n", "М141БПЛТЛ052 7\n", "М141БПЛТЛ011 6\n", "М141БПЛТЛ004 7\n", "М141БПЛТЛ010 6\n", "М141БПЛТЛ071 9\n", "М141БПЛТЛ035 6\n", "М141БПЛТЛ030 6\n", "М141БПЛТЛ070 5\n", "М141БПЛТЛ051 9\n", "М141БПЛТЛ046 7\n", "М141БПЛТЛ047 8\n", "М141БПЛТЛ063 5\n", "М141БПЛТЛ029 8\n", "М141БПЛТЛ064 8\n", "М141БПЛТЛ076 7\n", "М141БПЛТЛ062 7\n", "М141БПЛТЛ074 6\n", "130232038 7\n", "М141БПЛТЛ023 9\n", "М141БПЛТЛ054 8\n", "М141БПЛТЛ012 6\n", "М141БПЛТЛ006 5\n", "М141БПЛТЛ055 5\n", "М141БПЛТЛ007 7\n", "М141БПЛТЛ050 6\n", "М141БПЛТЛ066 10\n", "М141БПЛТЛ043 5\n", "М141БПЛТЛ084 7\n", "М141БПЛТЛ005 7\n", "М141БПЛТЛ044 5\n", "13051038 4\n", "Name: Mstat, dtype: int64" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['Mstat']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ещё столбец можно выбрать, не используя квадратные скобки, а просто указав его название через точку: " ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "id\n", "М141БПЛТЛ024 9\n", "М141БПЛТЛ031 10\n", "М141БПЛТЛ075 9\n", "М141БПЛТЛ017 9\n", "М141БПЛТЛ069 10\n", "М141БПЛТЛ072 9\n", "М141БПЛТЛ020 7\n", "М141БПЛТЛ026 10\n", "М141БПЛТЛ073 9\n", "М141БПЛТЛ078 6\n", "М141БПЛТЛ060 8\n", "М141БПЛТЛ040 9\n", "М141БПЛТЛ065 9\n", "М141БПЛТЛ053 7\n", "М141БПЛТЛ015 9\n", "М141БПЛТЛ021 9\n", "М141БПЛТЛ018 7\n", "М141БПЛТЛ039 8\n", "М141БПЛТЛ036 10\n", "М141БПЛТЛ049 7\n", "06114043 8\n", "М141БПЛТЛ048 6\n", "М141БПЛТЛ034 9\n", "М141БПЛТЛ045 8\n", "М141БПЛТЛ033 9\n", "М141БПЛТЛ083 5\n", "М141БПЛТЛ008 8\n", "М141БПЛТЛ001 7\n", "М141БПЛТЛ038 9\n", "М141БПЛТЛ052 7\n", "М141БПЛТЛ011 6\n", "М141БПЛТЛ004 7\n", "М141БПЛТЛ010 6\n", "М141БПЛТЛ071 9\n", "М141БПЛТЛ035 6\n", "М141БПЛТЛ030 6\n", "М141БПЛТЛ070 5\n", "М141БПЛТЛ051 9\n", "М141БПЛТЛ046 7\n", "М141БПЛТЛ047 8\n", "М141БПЛТЛ063 5\n", "М141БПЛТЛ029 8\n", "М141БПЛТЛ064 8\n", "М141БПЛТЛ076 7\n", "М141БПЛТЛ062 7\n", "М141БПЛТЛ074 6\n", "130232038 7\n", "М141БПЛТЛ023 9\n", "М141БПЛТЛ054 8\n", "М141БПЛТЛ012 6\n", "М141БПЛТЛ006 5\n", "М141БПЛТЛ055 5\n", "М141БПЛТЛ007 7\n", "М141БПЛТЛ050 6\n", "М141БПЛТЛ066 10\n", "М141БПЛТЛ043 5\n", "М141БПЛТЛ084 7\n", "М141БПЛТЛ005 7\n", "М141БПЛТЛ044 5\n", "13051038 4\n", "Name: Mstat, dtype: int64" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.Mstat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Однако такой способ не универсален. В случае, если в названии столбца используются недопустимые для переменных символы (пробелы, тире, кириллические буквы), этот метод не подойдет. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Если нам нужно выбрать более одного столбца, то названия столбцов указываются внутри списка – появляются двойные квадратные скобки:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SocPolsoc
id
М141БПЛТЛ02489
М141БПЛТЛ0311010
М141БПЛТЛ07599
М141БПЛТЛ01789
М141БПЛТЛ069109
М141БПЛТЛ07289
М141БПЛТЛ02079
М141БПЛТЛ02688
М141БПЛТЛ07389
М141БПЛТЛ07899
М141БПЛТЛ06078
М141БПЛТЛ04088
М141БПЛТЛ065810
М141БПЛТЛ05378
М141БПЛТЛ01577
М141БПЛТЛ02186
М141БПЛТЛ01898
М141БПЛТЛ03999
М141БПЛТЛ03677
М141БПЛТЛ04969
06114043109
М141БПЛТЛ04888
М141БПЛТЛ03476
М141БПЛТЛ04588
М141БПЛТЛ03388
М141БПЛТЛ08367
М141БПЛТЛ00889
М141БПЛТЛ00178
М141БПЛТЛ03868
М141БПЛТЛ05277
М141БПЛТЛ01187
М141БПЛТЛ00466
М141БПЛТЛ01078
М141БПЛТЛ07177
М141БПЛТЛ03577
М141БПЛТЛ03065
М141БПЛТЛ07065
М141БПЛТЛ05186
М141БПЛТЛ04677
М141БПЛТЛ04766
М141БПЛТЛ06365
М141БПЛТЛ02988
М141БПЛТЛ06464
М141БПЛТЛ07688
М141БПЛТЛ06276
М141БПЛТЛ07478
13023203865
М141БПЛТЛ02367
М141БПЛТЛ05468
М141БПЛТЛ01277
М141БПЛТЛ00667
М141БПЛТЛ05566
М141БПЛТЛ00776
М141БПЛТЛ05066
М141БПЛТЛ06676
М141БПЛТЛ04365
М141БПЛТЛ08484
М141БПЛТЛ00555
М141БПЛТЛ04474
1305103844
\n", "
" ], "text/plain": [ " Soc Polsoc\n", "id \n", "М141БПЛТЛ024 8 9\n", "М141БПЛТЛ031 10 10\n", "М141БПЛТЛ075 9 9\n", "М141БПЛТЛ017 8 9\n", "М141БПЛТЛ069 10 9\n", "М141БПЛТЛ072 8 9\n", "М141БПЛТЛ020 7 9\n", "М141БПЛТЛ026 8 8\n", "М141БПЛТЛ073 8 9\n", "М141БПЛТЛ078 9 9\n", "М141БПЛТЛ060 7 8\n", "М141БПЛТЛ040 8 8\n", "М141БПЛТЛ065 8 10\n", "М141БПЛТЛ053 7 8\n", "М141БПЛТЛ015 7 7\n", "М141БПЛТЛ021 8 6\n", "М141БПЛТЛ018 9 8\n", "М141БПЛТЛ039 9 9\n", "М141БПЛТЛ036 7 7\n", "М141БПЛТЛ049 6 9\n", "06114043 10 9\n", "М141БПЛТЛ048 8 8\n", "М141БПЛТЛ034 7 6\n", "М141БПЛТЛ045 8 8\n", "М141БПЛТЛ033 8 8\n", "М141БПЛТЛ083 6 7\n", "М141БПЛТЛ008 8 9\n", "М141БПЛТЛ001 7 8\n", "М141БПЛТЛ038 6 8\n", "М141БПЛТЛ052 7 7\n", "М141БПЛТЛ011 8 7\n", "М141БПЛТЛ004 6 6\n", "М141БПЛТЛ010 7 8\n", "М141БПЛТЛ071 7 7\n", "М141БПЛТЛ035 7 7\n", "М141БПЛТЛ030 6 5\n", "М141БПЛТЛ070 6 5\n", "М141БПЛТЛ051 8 6\n", "М141БПЛТЛ046 7 7\n", "М141БПЛТЛ047 6 6\n", "М141БПЛТЛ063 6 5\n", "М141БПЛТЛ029 8 8\n", "М141БПЛТЛ064 6 4\n", "М141БПЛТЛ076 8 8\n", "М141БПЛТЛ062 7 6\n", "М141БПЛТЛ074 7 8\n", "130232038 6 5\n", "М141БПЛТЛ023 6 7\n", "М141БПЛТЛ054 6 8\n", "М141БПЛТЛ012 7 7\n", "М141БПЛТЛ006 6 7\n", "М141БПЛТЛ055 6 6\n", "М141БПЛТЛ007 7 6\n", "М141БПЛТЛ050 6 6\n", "М141БПЛТЛ066 7 6\n", "М141БПЛТЛ043 6 5\n", "М141БПЛТЛ084 8 4\n", "М141БПЛТЛ005 5 5\n", "М141БПЛТЛ044 7 4\n", "13051038 4 4" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2[[\"Soc\", \"Polsoc\"]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Если нам нужно несколько столбцов подряд, начиная с одного названия и заканчивая другим, можно воспользоваться методом `.loc`: " ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
EconEngPolthMstat2PhistLaw
id
М141БПЛТЛ024898108.07
М141БПЛТЛ031101010109.09
М141БПЛТЛ0751091098.09
М141БПЛТЛ017899106.09
М141БПЛТЛ06910101098.08
М141БПЛТЛ072109898.08
М141БПЛТЛ020691088.07
М141БПЛТЛ026710798.08
М141БПЛТЛ07389898.08
М141БПЛТЛ078561076.08
М141БПЛТЛ06079885.07
М141БПЛТЛ04069786.09
М141БПЛТЛ06548879.08
М141БПЛТЛ05359878.08
М141БПЛТЛ01569794.07
М141БПЛТЛ02189887.07
М141БПЛТЛ01879786.06
М141БПЛТЛ03988868.07
М141БПЛТЛ03688694.08
М141БПЛТЛ04968684.08
06114043588810.07
М141БПЛТЛ04869644.06
М141БПЛТЛ03469686.07
М141БПЛТЛ04578676.07
М141БПЛТЛ03379797.07
М141БПЛТЛ08358765.07
М141БПЛТЛ008981098.09
М141БПЛТЛ001410776.08
М141БПЛТЛ03849676.07
М141БПЛТЛ05278666.08
М141БПЛТЛ01169665.06
М141БПЛТЛ00468665.05
М141БПЛТЛ01069776.07
М141БПЛТЛ07179684.06
М141БПЛТЛ03568554.06
М141БПЛТЛ03067664.08
М141БПЛТЛ07048655.06
М141БПЛТЛ05168767.06
М141БПЛТЛ04647585.07
М141БПЛТЛ04747595.06
М141БПЛТЛ06348444.05
М141БПЛТЛ02979567.06
М141БПЛТЛ06476684.06
М141БПЛТЛ07668666.08
М141БПЛТЛ06269665.06
М141БПЛТЛ07447656.06
13023203858484.08
М141БПЛТЛ02389694.07
М141БПЛТЛ05448644.06
М141БПЛТЛ012410654.07
М141БПЛТЛ00658555.06
М141БПЛТЛ05547748.05
М141БПЛТЛ00767674.05
М141БПЛТЛ05068454.05
М141БПЛТЛ06679584.06
М141БПЛТЛ04358565.06
М141БПЛТЛ0844855NaN8
М141БПЛТЛ00557474.05
М141БПЛТЛ04446445.04
1305103849555.05
\n", "
" ], "text/plain": [ " Econ Eng Polth Mstat2 Phist Law\n", "id \n", "М141БПЛТЛ024 8 9 8 10 8.0 7\n", "М141БПЛТЛ031 10 10 10 10 9.0 9\n", "М141БПЛТЛ075 10 9 10 9 8.0 9\n", "М141БПЛТЛ017 8 9 9 10 6.0 9\n", "М141БПЛТЛ069 10 10 10 9 8.0 8\n", "М141БПЛТЛ072 10 9 8 9 8.0 8\n", "М141БПЛТЛ020 6 9 10 8 8.0 7\n", "М141БПЛТЛ026 7 10 7 9 8.0 8\n", "М141БПЛТЛ073 8 9 8 9 8.0 8\n", "М141БПЛТЛ078 5 6 10 7 6.0 8\n", "М141БПЛТЛ060 7 9 8 8 5.0 7\n", "М141БПЛТЛ040 6 9 7 8 6.0 9\n", "М141БПЛТЛ065 4 8 8 7 9.0 8\n", "М141БПЛТЛ053 5 9 8 7 8.0 8\n", "М141БПЛТЛ015 6 9 7 9 4.0 7\n", "М141БПЛТЛ021 8 9 8 8 7.0 7\n", "М141БПЛТЛ018 7 9 7 8 6.0 6\n", "М141БПЛТЛ039 8 8 8 6 8.0 7\n", "М141БПЛТЛ036 8 8 6 9 4.0 8\n", "М141БПЛТЛ049 6 8 6 8 4.0 8\n", "06114043 5 8 8 8 10.0 7\n", "М141БПЛТЛ048 6 9 6 4 4.0 6\n", "М141БПЛТЛ034 6 9 6 8 6.0 7\n", "М141БПЛТЛ045 7 8 6 7 6.0 7\n", "М141БПЛТЛ033 7 9 7 9 7.0 7\n", "М141БПЛТЛ083 5 8 7 6 5.0 7\n", "М141БПЛТЛ008 9 8 10 9 8.0 9\n", "М141БПЛТЛ001 4 10 7 7 6.0 8\n", "М141БПЛТЛ038 4 9 6 7 6.0 7\n", "М141БПЛТЛ052 7 8 6 6 6.0 8\n", "М141БПЛТЛ011 6 9 6 6 5.0 6\n", "М141БПЛТЛ004 6 8 6 6 5.0 5\n", "М141БПЛТЛ010 6 9 7 7 6.0 7\n", "М141БПЛТЛ071 7 9 6 8 4.0 6\n", "М141БПЛТЛ035 6 8 5 5 4.0 6\n", "М141БПЛТЛ030 6 7 6 6 4.0 8\n", "М141БПЛТЛ070 4 8 6 5 5.0 6\n", "М141БПЛТЛ051 6 8 7 6 7.0 6\n", "М141БПЛТЛ046 4 7 5 8 5.0 7\n", "М141БПЛТЛ047 4 7 5 9 5.0 6\n", "М141БПЛТЛ063 4 8 4 4 4.0 5\n", "М141БПЛТЛ029 7 9 5 6 7.0 6\n", "М141БПЛТЛ064 7 6 6 8 4.0 6\n", "М141БПЛТЛ076 6 8 6 6 6.0 8\n", "М141БПЛТЛ062 6 9 6 6 5.0 6\n", "М141БПЛТЛ074 4 7 6 5 6.0 6\n", "130232038 5 8 4 8 4.0 8\n", "М141БПЛТЛ023 8 9 6 9 4.0 7\n", "М141БПЛТЛ054 4 8 6 4 4.0 6\n", "М141БПЛТЛ012 4 10 6 5 4.0 7\n", "М141БПЛТЛ006 5 8 5 5 5.0 6\n", "М141БПЛТЛ055 4 7 7 4 8.0 5\n", "М141БПЛТЛ007 6 7 6 7 4.0 5\n", "М141БПЛТЛ050 6 8 4 5 4.0 5\n", "М141БПЛТЛ066 7 9 5 8 4.0 6\n", "М141БПЛТЛ043 5 8 5 6 5.0 6\n", "М141БПЛТЛ084 4 8 5 5 NaN 8\n", "М141БПЛТЛ005 5 7 4 7 4.0 5\n", "М141БПЛТЛ044 4 6 4 4 5.0 4\n", "13051038 4 9 5 5 5.0 5" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[:, 'Econ' : 'Law']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Откуда в квадратных скобках взялось двоеточие? Дело в том, что метод `.loc` – более универсальный, и позволяет выбирать не только столбцы, но и строки. При этом нужные строки указываются на первом месте, а столбцы – на втором. Когда мы пишем `.loc[:, 1]`, мы сообщаем Python, что нам нужны все строки (`:`) и столбцы, начиная с `Econ` и до `Law` включительно.\n", "\n", "**Внимание:** выбор столбцов по названиям через двоеточие очень напоминает срезы (*slices*) в списках. Но есть важное отличие. В случае текстовых названий, оба конца среза (левый и правый) включаются. Если бы срезы по названиям были бы устроены как срезы по числовым индексам, код выше выдавал бы столбцы с `Econ` и до `Phist`, не включая колонку `Law`, так как в обычных срезах правый конец исключается." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Выбор столбцов по номеру**\n", "\n", "Иногда может возникнуть необходимость выбрать столбец по его порядковому номеру. Например, когда названий столбцов нет как таковых или когда названия слишком длинные, а переименовывать их нежелательно. Сделать это можно с помощью метода `.iloc`:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "id\n", "М141БПЛТЛ024 9\n", "М141БПЛТЛ031 10\n", "М141БПЛТЛ075 9\n", "М141БПЛТЛ017 9\n", "М141БПЛТЛ069 10\n", "М141БПЛТЛ072 9\n", "М141БПЛТЛ020 7\n", "М141БПЛТЛ026 10\n", "М141БПЛТЛ073 9\n", "М141БПЛТЛ078 6\n", "М141БПЛТЛ060 8\n", "М141БПЛТЛ040 9\n", "М141БПЛТЛ065 9\n", "М141БПЛТЛ053 7\n", "М141БПЛТЛ015 9\n", "М141БПЛТЛ021 9\n", "М141БПЛТЛ018 7\n", "М141БПЛТЛ039 8\n", "М141БПЛТЛ036 10\n", "М141БПЛТЛ049 7\n", "06114043 8\n", "М141БПЛТЛ048 6\n", "М141БПЛТЛ034 9\n", "М141БПЛТЛ045 8\n", "М141БПЛТЛ033 9\n", "М141БПЛТЛ083 5\n", "М141БПЛТЛ008 8\n", "М141БПЛТЛ001 7\n", "М141БПЛТЛ038 9\n", "М141БПЛТЛ052 7\n", "М141БПЛТЛ011 6\n", "М141БПЛТЛ004 7\n", "М141БПЛТЛ010 6\n", "М141БПЛТЛ071 9\n", "М141БПЛТЛ035 6\n", "М141БПЛТЛ030 6\n", "М141БПЛТЛ070 5\n", "М141БПЛТЛ051 9\n", "М141БПЛТЛ046 7\n", "М141БПЛТЛ047 8\n", "М141БПЛТЛ063 5\n", "М141БПЛТЛ029 8\n", "М141БПЛТЛ064 8\n", "М141БПЛТЛ076 7\n", "М141БПЛТЛ062 7\n", "М141БПЛТЛ074 6\n", "130232038 7\n", "М141БПЛТЛ023 9\n", "М141БПЛТЛ054 8\n", "М141БПЛТЛ012 6\n", "М141БПЛТЛ006 5\n", "М141БПЛТЛ055 5\n", "М141БПЛТЛ007 7\n", "М141БПЛТЛ050 6\n", "М141БПЛТЛ066 10\n", "М141БПЛТЛ043 5\n", "М141БПЛТЛ084 7\n", "М141БПЛТЛ005 7\n", "М141БПЛТЛ044 5\n", "13051038 4\n", "Name: Mstat, dtype: int64" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iloc[:, 1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Синтаксис кода с `.iloc` несильно отличается от синтаксиса `.loc`. В чем разница? Разница заключается в том, что метод `.loc` работает с текстовыми названиями, а метод `.iloc` – с числовыми индексами. Отсюда и префикс `i` в названии (*i* – индекс, *loc* – location). Если мы попытаемся в `.iloc` указать названия столбцов, Python выдаст ошибку:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "cannot do slice indexing on with these indexers [Mstat] of ", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Mstat'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'Econ'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 1470\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mKeyError\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mIndexError\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1471\u001b[0m \u001b[0;32mpass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1472\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_tuple\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1473\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1474\u001b[0m \u001b[0;31m# we by definition only have the 0th axis\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_getitem_tuple\u001b[0;34m(self, tup)\u001b[0m\n\u001b[1;32m 2027\u001b[0m \u001b[0;32mcontinue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2028\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2029\u001b[0;31m \u001b[0mretval\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mretval\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2030\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2031\u001b[0m \u001b[0;31m# if the dim was reduced, then pass a lower-dim the next time\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_getitem_axis\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m 2078\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2079\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mslice\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2080\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_slice_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2081\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2082\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_get_slice_axis\u001b[0;34m(self, slice_obj, axis)\u001b[0m\n\u001b[1;32m 2046\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdeep\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2047\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2048\u001b[0;31m \u001b[0mslice_obj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_convert_slice_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mslice_obj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2049\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mslice_obj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mslice\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2050\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mslice_obj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkind\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'iloc'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_convert_slice_indexer\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m 264\u001b[0m \u001b[0;31m# if we are accessing via lowered dim, use the last dim\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 265\u001b[0m \u001b[0max\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 266\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0max\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_convert_slice_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkind\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 267\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 268\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_has_valid_setitem_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36m_convert_slice_indexer\u001b[0;34m(self, key, kind)\u001b[0m\n\u001b[1;32m 1688\u001b[0m \u001b[0;31m# validate iloc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1689\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mkind\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'iloc'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1690\u001b[0;31m return slice(self._validate_indexer('slice', key.start, kind),\n\u001b[0m\u001b[1;32m 1691\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_validate_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'slice'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstop\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkind\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1692\u001b[0m self._validate_indexer('slice', key.step, kind))\n", "\u001b[0;32m/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36m_validate_indexer\u001b[0;34m(self, form, key, kind)\u001b[0m\n\u001b[1;32m 4126\u001b[0m \u001b[0;32mpass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4127\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mkind\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'iloc'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'getitem'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 4128\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_invalid_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mform\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4129\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4130\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36m_invalid_indexer\u001b[0;34m(self, form, key)\u001b[0m\n\u001b[1;32m 1846\u001b[0m \"indexers [{key}] of {kind}\".format(\n\u001b[1;32m 1847\u001b[0m \u001b[0mform\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mform\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mklass\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1848\u001b[0;31m kind=type(key)))\n\u001b[0m\u001b[1;32m 1849\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1850\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mget_duplicates\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: cannot do slice indexing on with these indexers [Mstat] of " ] } ], "source": [ "df.iloc[:, 'Mstat': 'Econ']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python пишет, что невозможно взять срез по индексам, которые имеют строковый тип (`class 'str'`), так как в квадратных скобках ожидаются числовые (целочисленные) индексы.\n", "\n", "Если нужно выбрать несколько столбцов подряд, можно воспользоваться срезами:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
MstatSoc
id
М141БПЛТЛ02498
М141БПЛТЛ0311010
М141БПЛТЛ07599
М141БПЛТЛ01798
М141БПЛТЛ0691010
М141БПЛТЛ07298
М141БПЛТЛ02077
М141БПЛТЛ026108
М141БПЛТЛ07398
М141БПЛТЛ07869
М141БПЛТЛ06087
М141БПЛТЛ04098
М141БПЛТЛ06598
М141БПЛТЛ05377
М141БПЛТЛ01597
М141БПЛТЛ02198
М141БПЛТЛ01879
М141БПЛТЛ03989
М141БПЛТЛ036107
М141БПЛТЛ04976
06114043810
М141БПЛТЛ04868
М141БПЛТЛ03497
М141БПЛТЛ04588
М141БПЛТЛ03398
М141БПЛТЛ08356
М141БПЛТЛ00888
М141БПЛТЛ00177
М141БПЛТЛ03896
М141БПЛТЛ05277
М141БПЛТЛ01168
М141БПЛТЛ00476
М141БПЛТЛ01067
М141БПЛТЛ07197
М141БПЛТЛ03567
М141БПЛТЛ03066
М141БПЛТЛ07056
М141БПЛТЛ05198
М141БПЛТЛ04677
М141БПЛТЛ04786
М141БПЛТЛ06356
М141БПЛТЛ02988
М141БПЛТЛ06486
М141БПЛТЛ07678
М141БПЛТЛ06277
М141БПЛТЛ07467
13023203876
М141БПЛТЛ02396
М141БПЛТЛ05486
М141БПЛТЛ01267
М141БПЛТЛ00656
М141БПЛТЛ05556
М141БПЛТЛ00777
М141БПЛТЛ05066
М141БПЛТЛ066107
М141БПЛТЛ04356
М141БПЛТЛ08478
М141БПЛТЛ00575
М141БПЛТЛ04457
1305103844
\n", "
" ], "text/plain": [ " Mstat Soc\n", "id \n", "М141БПЛТЛ024 9 8\n", "М141БПЛТЛ031 10 10\n", "М141БПЛТЛ075 9 9\n", "М141БПЛТЛ017 9 8\n", "М141БПЛТЛ069 10 10\n", "М141БПЛТЛ072 9 8\n", "М141БПЛТЛ020 7 7\n", "М141БПЛТЛ026 10 8\n", "М141БПЛТЛ073 9 8\n", "М141БПЛТЛ078 6 9\n", "М141БПЛТЛ060 8 7\n", "М141БПЛТЛ040 9 8\n", "М141БПЛТЛ065 9 8\n", "М141БПЛТЛ053 7 7\n", "М141БПЛТЛ015 9 7\n", "М141БПЛТЛ021 9 8\n", "М141БПЛТЛ018 7 9\n", "М141БПЛТЛ039 8 9\n", "М141БПЛТЛ036 10 7\n", "М141БПЛТЛ049 7 6\n", "06114043 8 10\n", "М141БПЛТЛ048 6 8\n", "М141БПЛТЛ034 9 7\n", "М141БПЛТЛ045 8 8\n", "М141БПЛТЛ033 9 8\n", "М141БПЛТЛ083 5 6\n", "М141БПЛТЛ008 8 8\n", "М141БПЛТЛ001 7 7\n", "М141БПЛТЛ038 9 6\n", "М141БПЛТЛ052 7 7\n", "М141БПЛТЛ011 6 8\n", "М141БПЛТЛ004 7 6\n", "М141БПЛТЛ010 6 7\n", "М141БПЛТЛ071 9 7\n", "М141БПЛТЛ035 6 7\n", "М141БПЛТЛ030 6 6\n", "М141БПЛТЛ070 5 6\n", "М141БПЛТЛ051 9 8\n", "М141БПЛТЛ046 7 7\n", "М141БПЛТЛ047 8 6\n", "М141БПЛТЛ063 5 6\n", "М141БПЛТЛ029 8 8\n", "М141БПЛТЛ064 8 6\n", "М141БПЛТЛ076 7 8\n", "М141БПЛТЛ062 7 7\n", "М141БПЛТЛ074 6 7\n", "130232038 7 6\n", "М141БПЛТЛ023 9 6\n", "М141БПЛТЛ054 8 6\n", "М141БПЛТЛ012 6 7\n", "М141БПЛТЛ006 5 6\n", "М141БПЛТЛ055 5 6\n", "М141БПЛТЛ007 7 7\n", "М141БПЛТЛ050 6 6\n", "М141БПЛТЛ066 10 7\n", "М141БПЛТЛ043 5 6\n", "М141БПЛТЛ084 7 8\n", "М141БПЛТЛ005 7 5\n", "М141БПЛТЛ044 5 7\n", "13051038 4 4" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iloc[:, 1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Числовые срезы в `pandas` уже ничем не отличаются от списковых срезов: правый конец среза не включается. В нашем случае мы выбрали только столбцы с индексами 1 и 2." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Выбор строк по названию**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Выбор строки по названию происходит аналогичным образом, только здесь метод `.loc` уже обязателен." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Cps 8.0\n", "Mstat 10.0\n", "Soc 10.0\n", "Econ 10.0\n", "Eng 10.0\n", "Polth 10.0\n", "Mstat2 10.0\n", "Phist 9.0\n", "Law 9.0\n", "Phil 10.0\n", "Polsoc 10.0\n", "Ptheo 9.0\n", "Preg 8.0\n", "Compp 8.0\n", "Game 9.0\n", "Wpol 10.0\n", "Male 1.0\n", "Name: М141БПЛТЛ031, dtype: float64" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc['М141БПЛТЛ031'] # строка для студента с номером М141БПЛТЛ031" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "При этом ставить запятую и двоеточие, показывая, что нам нужна одна строка и все столбцы, уже не нужно. Если нам нужно выбрать несколько строк подряд, то `.loc` не нужен:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMale
id
М141БПЛТЛ024798898108.07997.088.06101
М141БПЛТЛ03181010101010109.0910109.088.09101
М141БПЛТЛ0759991091098.091099.088.0791
М141БПЛТЛ017998899106.09998.088.0890
М141БПЛТЛ06910101010101098.081097.065.08101
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ024 7 9 8 8 9 8 10 8.0 7 9 \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 10 \n", "М141БПЛТЛ075 9 9 9 10 9 10 9 8.0 9 10 \n", "М141БПЛТЛ017 9 9 8 8 9 9 10 6.0 9 9 \n", "М141БПЛТЛ069 10 10 10 10 10 10 9 8.0 8 10 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ024 9 7.0 8 8.0 6 10 1 \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 \n", "М141БПЛТЛ075 9 9.0 8 8.0 7 9 1 \n", "М141БПЛТЛ017 9 8.0 8 8.0 8 9 0 \n", "М141БПЛТЛ069 9 7.0 6 5.0 8 10 1 " ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[\"М141БПЛТЛ024\":'М141БПЛТЛ069']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Как Python понимает, что мы просим вывести именно строки с такими названиями, а не столбцы? Потому что у нас стоят одинарные квадратные скобки, а не двойные, как в случае со столбцами. (Да, в `pandas` много всяких тонкостей, но чтобы хорошо в них разбираться, нужно просто попрактиковаться и привыкнуть).\n", "\n", "Обратите внимание: разницы между двойными и одинарными кавычками нет, строки можно вводить в любых кавычках, как в примере выше." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Выбор строк по номеру**\n", "\n", "В этом случае достаточно указать номер в квадратных скобках в `.iloc`:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Cps 9.0\n", "Mstat 9.0\n", "Soc 9.0\n", "Econ 10.0\n", "Eng 9.0\n", "Polth 10.0\n", "Mstat2 9.0\n", "Phist 8.0\n", "Law 9.0\n", "Phil 10.0\n", "Polsoc 9.0\n", "Ptheo 9.0\n", "Preg 8.0\n", "Compp 8.0\n", "Game 7.0\n", "Wpol 9.0\n", "Male 1.0\n", "Name: М141БПЛТЛ075, dtype: float64" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iloc[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Если нужно несколько строк подряд, можно воспользоваться срезами:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMale
id
М141БПЛТЛ03181010101010109.0910109.088.09101
М141БПЛТЛ0759991091098.091099.088.0791
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 10 \n", "М141БПЛТЛ075 9 9 9 10 9 10 9 8.0 9 10 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 \n", "М141БПЛТЛ075 9 9.0 8 8.0 7 9 1 " ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[1:3] # и без iloc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Если нужно несколько строк не подряд, можно просто перечислить внутри списка в `.iloc`:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMale
id
М141БПЛТЛ03181010101010109.0910109.088.09101
М141БПЛТЛ0759991091098.091099.088.0791
М141БПЛТЛ0721098109898.081097.088.0990
М141БПЛТЛ06078779885.07585.078.0791
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 10 \n", "М141БПЛТЛ075 9 9 9 10 9 10 9 8.0 9 10 \n", "М141БПЛТЛ072 10 9 8 10 9 8 9 8.0 8 10 \n", "М141БПЛТЛ060 7 8 7 7 9 8 8 5.0 7 5 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 \n", "М141БПЛТЛ075 9 9.0 8 8.0 7 9 1 \n", "М141БПЛТЛ072 9 7.0 8 8.0 9 9 0 \n", "М141БПЛТЛ060 8 5.0 7 8.0 7 9 1 " ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iloc[[1, 2, 5, 10]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Выбор наблюдений по названиям строк и столбцов**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Если нам нужно выбрать одно наблюдение на пересечении строки и столбца, можно воспользоваться методом `.at`: сначала указать название строки, потом ‒ столбца:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.at['М141БПЛТЛ078', 'Game'] # оценка по теории игр у студента М141БПЛТЛ078" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Кроме того, можно применить метод `.loc`:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[\"М141БПЛТЛ075\", \"Soc\"] # оценка по социологии у студента М141БПЛТЛ075" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "В чем разница между `.at` и `.loc`? Метод `.loc` более универсален. В то время как `.at` используется для нахождения *одного* наблюдения на пересечении строки и столбца, `.loc` позволяет выбрать несколько наблюдений (строк и столбцов) сразу. Например, так:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "id\n", "М141БПЛТЛ024 9\n", "М141БПЛТЛ031 10\n", "М141БПЛТЛ075 9\n", "М141БПЛТЛ017 9\n", "М141БПЛТЛ069 10\n", "М141БПЛТЛ072 9\n", "М141БПЛТЛ020 7\n", "М141БПЛТЛ026 10\n", "М141БПЛТЛ073 9\n", "Name: Mstat, dtype: int64" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[\"М141БПЛТЛ024\":\"М141БПЛТЛ073\", \"Mstat\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Если нужно выбрать какое-то одно значение, метод `.at` будет работать более быстро, чем `.loc`. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Выбор наблюдения по номеру строки и столбца **" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Выбор наблюдения по номеру строки и столбца осуществляется аналогичным образом, только теперь мы используем методы с префиксом `i` для индексов: `.iat` и `.iloc`." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iat[4, 6] # оценка на пересечении строки 4 и столбца 6" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iloc[8, 4] # оценка на пересечении строки 8 и столбца 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Убедимся, что все верно:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMale
id
М141БПЛТЛ024798898108.07997.088.06101
М141БПЛТЛ03181010101010109.0910109.088.09101
М141БПЛТЛ0759991091098.091099.088.0791
М141БПЛТЛ017998899106.09998.088.0890
М141БПЛТЛ06910101010101098.081097.065.08101
М141БПЛТЛ0721098109898.081097.088.0990
М141БПЛТЛ020877691088.07797.086.0891
М141БПЛТЛ0267108710798.08888.087.0780
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ024 7 9 8 8 9 8 10 8.0 7 9 \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 10 \n", "М141БПЛТЛ075 9 9 9 10 9 10 9 8.0 9 10 \n", "М141БПЛТЛ017 9 9 8 8 9 9 10 6.0 9 9 \n", "М141БПЛТЛ069 10 10 10 10 10 10 9 8.0 8 10 \n", "М141БПЛТЛ072 10 9 8 10 9 8 9 8.0 8 10 \n", "М141БПЛТЛ020 8 7 7 6 9 10 8 8.0 7 7 \n", "М141БПЛТЛ026 7 10 8 7 10 7 9 8.0 8 8 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ024 9 7.0 8 8.0 6 10 1 \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 \n", "М141БПЛТЛ075 9 9.0 8 8.0 7 9 1 \n", "М141БПЛТЛ017 9 8.0 8 8.0 8 9 0 \n", "М141БПЛТЛ069 9 7.0 6 5.0 8 10 1 \n", "М141БПЛТЛ072 9 7.0 8 8.0 9 9 0 \n", "М141БПЛТЛ020 9 7.0 8 6.0 8 9 1 \n", "М141БПЛТЛ026 8 8.0 8 7.0 7 8 0 " ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head(8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Выбор строк по условию (фильтрация наблюдений)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Часто в исследованиях нас не интересует выбор отдельных строк по названию или номеру, мы хотим отбирать строки в таблице согласно некорому условию (условиям). Другими словами, проводить фильтрацию наблюдений. Для этого интересующее нас условие необходимо указать в квадратных скобках. Выберем из датафрейма `df`строки, которые соответствуют студентам с оценкой по экономике выше 6." ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMale
id
М141БПЛТЛ024798898108.07997.088.06101
М141БПЛТЛ03181010101010109.0910109.088.09101
М141БПЛТЛ0759991091098.091099.088.0791
М141БПЛТЛ017998899106.09998.088.0890
М141БПЛТЛ06910101010101098.081097.065.08101
М141БПЛТЛ0721098109898.081097.088.0990
М141БПЛТЛ0267108710798.08888.087.0780
М141БПЛТЛ07379889898.08997.076.01091
М141БПЛТЛ06078779885.07585.078.0791
М141БПЛТЛ02189889887.07766.086.0780
М141БПЛТЛ01877979786.06787.077.0780
М141БПЛТЛ03998988868.07696.078.0491
М141БПЛТЛ036810788694.08876.076.0781
М141БПЛТЛ04558878676.07786.086.0580
М141БПЛТЛ03359879797.07887.085.0780
М141БПЛТЛ0081088981098.091098.055.01041
М141БПЛТЛ05277778666.08675.086.0571
М141БПЛТЛ07169779684.06776.05NaN570
М141БПЛТЛ02968879567.06585.074.0570
М141БПЛТЛ06478676684.06444.065.0470
М141БПЛТЛ02379689694.07776.044.0751
М141БПЛТЛ066710779584.06564.064.0560
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ024 7 9 8 8 9 8 10 8.0 7 9 \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 10 \n", "М141БПЛТЛ075 9 9 9 10 9 10 9 8.0 9 10 \n", "М141БПЛТЛ017 9 9 8 8 9 9 10 6.0 9 9 \n", "М141БПЛТЛ069 10 10 10 10 10 10 9 8.0 8 10 \n", "М141БПЛТЛ072 10 9 8 10 9 8 9 8.0 8 10 \n", "М141БПЛТЛ026 7 10 8 7 10 7 9 8.0 8 8 \n", "М141БПЛТЛ073 7 9 8 8 9 8 9 8.0 8 9 \n", "М141БПЛТЛ060 7 8 7 7 9 8 8 5.0 7 5 \n", "М141БПЛТЛ021 8 9 8 8 9 8 8 7.0 7 7 \n", "М141БПЛТЛ018 7 7 9 7 9 7 8 6.0 6 7 \n", "М141БПЛТЛ039 9 8 9 8 8 8 6 8.0 7 6 \n", "М141БПЛТЛ036 8 10 7 8 8 6 9 4.0 8 8 \n", "М141БПЛТЛ045 5 8 8 7 8 6 7 6.0 7 7 \n", "М141БПЛТЛ033 5 9 8 7 9 7 9 7.0 7 8 \n", "М141БПЛТЛ008 10 8 8 9 8 10 9 8.0 9 10 \n", "М141БПЛТЛ052 7 7 7 7 8 6 6 6.0 8 6 \n", "М141БПЛТЛ071 6 9 7 7 9 6 8 4.0 6 7 \n", "М141БПЛТЛ029 6 8 8 7 9 5 6 7.0 6 5 \n", "М141БПЛТЛ064 7 8 6 7 6 6 8 4.0 6 4 \n", "М141БПЛТЛ023 7 9 6 8 9 6 9 4.0 7 7 \n", "М141БПЛТЛ066 7 10 7 7 9 5 8 4.0 6 5 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ024 9 7.0 8 8.0 6 10 1 \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 \n", "М141БПЛТЛ075 9 9.0 8 8.0 7 9 1 \n", "М141БПЛТЛ017 9 8.0 8 8.0 8 9 0 \n", "М141БПЛТЛ069 9 7.0 6 5.0 8 10 1 \n", "М141БПЛТЛ072 9 7.0 8 8.0 9 9 0 \n", "М141БПЛТЛ026 8 8.0 8 7.0 7 8 0 \n", "М141БПЛТЛ073 9 7.0 7 6.0 10 9 1 \n", "М141БПЛТЛ060 8 5.0 7 8.0 7 9 1 \n", "М141БПЛТЛ021 6 6.0 8 6.0 7 8 0 \n", "М141БПЛТЛ018 8 7.0 7 7.0 7 8 0 \n", "М141БПЛТЛ039 9 6.0 7 8.0 4 9 1 \n", "М141БПЛТЛ036 7 6.0 7 6.0 7 8 1 \n", "М141БПЛТЛ045 8 6.0 8 6.0 5 8 0 \n", "М141БПЛТЛ033 8 7.0 8 5.0 7 8 0 \n", "М141БПЛТЛ008 9 8.0 5 5.0 10 4 1 \n", "М141БПЛТЛ052 7 5.0 8 6.0 5 7 1 \n", "М141БПЛТЛ071 7 6.0 5 NaN 5 7 0 \n", "М141БПЛТЛ029 8 5.0 7 4.0 5 7 0 \n", "М141БПЛТЛ064 4 4.0 6 5.0 4 7 0 \n", "М141БПЛТЛ023 7 6.0 4 4.0 7 5 1 \n", "М141БПЛТЛ066 6 4.0 6 4.0 5 6 0 " ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[df[\"Econ\"] > 6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Почему нельзя было написать проще, то есть `df[\"Econ\"] > 6`? Давайте напишем, и посмотрим, что получится:" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "id\n", "М141БПЛТЛ024 True\n", "М141БПЛТЛ031 True\n", "М141БПЛТЛ075 True\n", "М141БПЛТЛ017 True\n", "М141БПЛТЛ069 True\n", "М141БПЛТЛ072 True\n", "М141БПЛТЛ020 False\n", "М141БПЛТЛ026 True\n", "М141БПЛТЛ073 True\n", "М141БПЛТЛ078 False\n", "М141БПЛТЛ060 True\n", "М141БПЛТЛ040 False\n", "М141БПЛТЛ065 False\n", "М141БПЛТЛ053 False\n", "М141БПЛТЛ015 False\n", "М141БПЛТЛ021 True\n", "М141БПЛТЛ018 True\n", "М141БПЛТЛ039 True\n", "М141БПЛТЛ036 True\n", "М141БПЛТЛ049 False\n", "06114043 False\n", "М141БПЛТЛ048 False\n", "М141БПЛТЛ034 False\n", "М141БПЛТЛ045 True\n", "М141БПЛТЛ033 True\n", "М141БПЛТЛ083 False\n", "М141БПЛТЛ008 True\n", "М141БПЛТЛ001 False\n", "М141БПЛТЛ038 False\n", "М141БПЛТЛ052 True\n", "М141БПЛТЛ011 False\n", "М141БПЛТЛ004 False\n", "М141БПЛТЛ010 False\n", "М141БПЛТЛ071 True\n", "М141БПЛТЛ035 False\n", "М141БПЛТЛ030 False\n", "М141БПЛТЛ070 False\n", "М141БПЛТЛ051 False\n", "М141БПЛТЛ046 False\n", "М141БПЛТЛ047 False\n", "М141БПЛТЛ063 False\n", "М141БПЛТЛ029 True\n", "М141БПЛТЛ064 True\n", "М141БПЛТЛ076 False\n", "М141БПЛТЛ062 False\n", "М141БПЛТЛ074 False\n", "130232038 False\n", "М141БПЛТЛ023 True\n", "М141БПЛТЛ054 False\n", "М141БПЛТЛ012 False\n", "М141БПЛТЛ006 False\n", "М141БПЛТЛ055 False\n", "М141БПЛТЛ007 False\n", "М141БПЛТЛ050 False\n", "М141БПЛТЛ066 True\n", "М141БПЛТЛ043 False\n", "М141БПЛТЛ084 False\n", "М141БПЛТЛ005 False\n", "М141БПЛТЛ044 False\n", "13051038 False\n", "Name: Econ, dtype: bool" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[\"Econ\"] > 6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Что мы увидели? Просто результат проверки условия, набор из *True* и *False*. Когда мы подставляем это выражение в квадратные скобки, Python выбирает из `df` те строки, где выражение принимает значение *True*.\n", "\n", "Все операторы проверки условий работают как обычно:" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMale
id
М141БПЛТЛ0081088981098.091098.055.01041
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ008 10 8 8 9 8 10 9 8.0 9 10 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ008 9 8.0 5 5.0 10 4 1 " ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[df[\"Econ\"] == 9] # двойное равенство для равенства" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Можно формулировать сложные условия. Выберем студентов с оценкой по экономике от 6 до 8 (8 не включается)." ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMale
id
М141БПЛТЛ020877691088.07797.086.0891
М141БПЛТЛ0267108710798.08888.087.0780
М141БПЛТЛ06078779885.07585.078.0791
М141БПЛТЛ04069869786.09585.085.07100
М141БПЛТЛ01569769794.07776.077.01070
М141БПЛТЛ01877979786.06787.077.0780
М141БПЛТЛ04967668684.08596.085.0680
М141БПЛТЛ04886869644.06484.067.0780
М141БПЛТЛ03469769686.07665.085.0890
М141БПЛТЛ04558878676.07786.086.0580
М141БПЛТЛ03359879797.07887.085.0780
М141БПЛТЛ05277778666.08675.086.0571
М141БПЛТЛ01176869665.06676.086.0580
М141БПЛТЛ00477668665.05565.075.0880
М141БПЛТЛ01066769776.07586.086.0581
М141БПЛТЛ07169779684.06776.05NaN570
М141БПЛТЛ03556768554.06675.087.0670
М141БПЛТЛ03076667664.08555.085.0791
М141БПЛТЛ05189868767.06665.044.0551
М141БПЛТЛ02968879567.06585.074.0570
М141БПЛТЛ06478676684.06444.065.0470
М141БПЛТЛ07677868666.08685.074.0460
М141БПЛТЛ06277769665.06564.055.0460
М141БПЛТЛ00767767674.05565.045.0471
М141БПЛТЛ05086668454.05564.054.0660
М141БПЛТЛ066710779584.06564.064.0560
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ020 8 7 7 6 9 10 8 8.0 7 7 \n", "М141БПЛТЛ026 7 10 8 7 10 7 9 8.0 8 8 \n", "М141БПЛТЛ060 7 8 7 7 9 8 8 5.0 7 5 \n", "М141БПЛТЛ040 6 9 8 6 9 7 8 6.0 9 5 \n", "М141БПЛТЛ015 6 9 7 6 9 7 9 4.0 7 7 \n", "М141БПЛТЛ018 7 7 9 7 9 7 8 6.0 6 7 \n", "М141БПЛТЛ049 6 7 6 6 8 6 8 4.0 8 5 \n", "М141БПЛТЛ048 8 6 8 6 9 6 4 4.0 6 4 \n", "М141БПЛТЛ034 6 9 7 6 9 6 8 6.0 7 6 \n", "М141БПЛТЛ045 5 8 8 7 8 6 7 6.0 7 7 \n", "М141БПЛТЛ033 5 9 8 7 9 7 9 7.0 7 8 \n", "М141БПЛТЛ052 7 7 7 7 8 6 6 6.0 8 6 \n", "М141БПЛТЛ011 7 6 8 6 9 6 6 5.0 6 6 \n", "М141БПЛТЛ004 7 7 6 6 8 6 6 5.0 5 5 \n", "М141БПЛТЛ010 6 6 7 6 9 7 7 6.0 7 5 \n", "М141БПЛТЛ071 6 9 7 7 9 6 8 4.0 6 7 \n", "М141БПЛТЛ035 5 6 7 6 8 5 5 4.0 6 6 \n", "М141БПЛТЛ030 7 6 6 6 7 6 6 4.0 8 5 \n", "М141БПЛТЛ051 8 9 8 6 8 7 6 7.0 6 6 \n", "М141БПЛТЛ029 6 8 8 7 9 5 6 7.0 6 5 \n", "М141БПЛТЛ064 7 8 6 7 6 6 8 4.0 6 4 \n", "М141БПЛТЛ076 7 7 8 6 8 6 6 6.0 8 6 \n", "М141БПЛТЛ062 7 7 7 6 9 6 6 5.0 6 5 \n", "М141БПЛТЛ007 6 7 7 6 7 6 7 4.0 5 5 \n", "М141БПЛТЛ050 8 6 6 6 8 4 5 4.0 5 5 \n", "М141БПЛТЛ066 7 10 7 7 9 5 8 4.0 6 5 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ020 9 7.0 8 6.0 8 9 1 \n", "М141БПЛТЛ026 8 8.0 8 7.0 7 8 0 \n", "М141БПЛТЛ060 8 5.0 7 8.0 7 9 1 \n", "М141БПЛТЛ040 8 5.0 8 5.0 7 10 0 \n", "М141БПЛТЛ015 7 6.0 7 7.0 10 7 0 \n", "М141БПЛТЛ018 8 7.0 7 7.0 7 8 0 \n", "М141БПЛТЛ049 9 6.0 8 5.0 6 8 0 \n", "М141БПЛТЛ048 8 4.0 6 7.0 7 8 0 \n", "М141БПЛТЛ034 6 5.0 8 5.0 8 9 0 \n", "М141БПЛТЛ045 8 6.0 8 6.0 5 8 0 \n", "М141БПЛТЛ033 8 7.0 8 5.0 7 8 0 \n", "М141БПЛТЛ052 7 5.0 8 6.0 5 7 1 \n", "М141БПЛТЛ011 7 6.0 8 6.0 5 8 0 \n", "М141БПЛТЛ004 6 5.0 7 5.0 8 8 0 \n", "М141БПЛТЛ010 8 6.0 8 6.0 5 8 1 \n", "М141БПЛТЛ071 7 6.0 5 NaN 5 7 0 \n", "М141БПЛТЛ035 7 5.0 8 7.0 6 7 0 \n", "М141БПЛТЛ030 5 5.0 8 5.0 7 9 1 \n", "М141БПЛТЛ051 6 5.0 4 4.0 5 5 1 \n", "М141БПЛТЛ029 8 5.0 7 4.0 5 7 0 \n", "М141БПЛТЛ064 4 4.0 6 5.0 4 7 0 \n", "М141БПЛТЛ076 8 5.0 7 4.0 4 6 0 \n", "М141БПЛТЛ062 6 4.0 5 5.0 4 6 0 \n", "М141БПЛТЛ007 6 5.0 4 5.0 4 7 1 \n", "М141БПЛТЛ050 6 4.0 5 4.0 6 6 0 \n", "М141БПЛТЛ066 6 4.0 6 4.0 5 6 0 " ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[(df[\"Econ\"] >= 6) & (df[\"Econ\"] < 8)] " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "В качестве символа для одновременного выполнения условий используется оператор `&`. И не забудьте про круглые скобки. А теперь выберем студентов с оценкой по английскому выше 9 и оценкой по праву ниже 9:" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMale
id
М141БПЛТЛ06910101010101098.081097.065.08101
М141БПЛТЛ0267108710798.08888.087.0780
М141БПЛТЛ001677410776.08684.066.0480
М141БПЛТЛ012667410654.07574.054.0481
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ069 10 10 10 10 10 10 9 8.0 8 10 \n", "М141БПЛТЛ026 7 10 8 7 10 7 9 8.0 8 8 \n", "М141БПЛТЛ001 6 7 7 4 10 7 7 6.0 8 6 \n", "М141БПЛТЛ012 6 6 7 4 10 6 5 4.0 7 5 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ069 9 7.0 6 5.0 8 10 1 \n", "М141БПЛТЛ026 8 8.0 8 7.0 7 8 0 \n", "М141БПЛТЛ001 8 4.0 6 6.0 4 8 0 \n", "М141БПЛТЛ012 7 4.0 5 4.0 4 8 1 " ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[(df[\"Eng\"] > 9) & (df[\"Law\"] < 9)] " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "При формулировании сложных (составных) условий обращайте особое внимание на порядок круглых скобках, потому что, если вы расставите скобки неправильно, результат получится неверный:" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMale
id
М141БПЛТЛ024798898108.07997.088.06101
М141БПЛТЛ03181010101010109.0910109.088.09101
М141БПЛТЛ0759991091098.091099.088.0791
М141БПЛТЛ017998899106.09998.088.0890
М141БПЛТЛ06910101010101098.081097.065.08101
М141БПЛТЛ0721098109898.081097.088.0990
М141БПЛТЛ020877691088.07797.086.0891
М141БПЛТЛ0267108710798.08888.087.0780
М141БПЛТЛ07379889898.08997.076.01091
М141БПЛТЛ078669561076.08696.088.0670
М141БПЛТЛ06078779885.07585.078.0791
М141БПЛТЛ04069869786.09585.085.07100
М141БПЛТЛ06599848879.085109.088.0691
М141БПЛТЛ05367759878.08687.086.0990
М141БПЛТЛ01569769794.07776.077.01070
М141БПЛТЛ02189889887.07766.086.0780
М141БПЛТЛ01877979786.06787.077.0780
М141БПЛТЛ03998988868.07696.078.0491
М141БПЛТЛ036810788694.08876.076.0781
М141БПЛТЛ04967668684.08596.085.0680
061140438810588810.0779NaN78.0781
М141БПЛТЛ04886869644.06484.067.0780
М141БПЛТЛ03469769686.07665.085.0890
М141БПЛТЛ04558878676.07786.086.0580
М141БПЛТЛ03359879797.07887.085.0780
М141БПЛТЛ08355658765.07575.075.0470
М141БПЛТЛ0081088981098.091098.055.01041
М141БПЛТЛ001677410776.08684.066.0480
М141БПЛТЛ03879649676.07484.054.0971
М141БПЛТЛ05277778666.08675.086.0571
М141БПЛТЛ01176869665.06676.086.0580
М141БПЛТЛ00477668665.05565.075.0880
М141БПЛТЛ01066769776.07586.086.0581
М141БПЛТЛ07169779684.06776.05NaN570
М141БПЛТЛ03556768554.06675.087.0670
М141БПЛТЛ03076667664.08555.085.0791
М141БПЛТЛ07055648655.06456.085.0670
М141БПЛТЛ05189868767.06665.044.0551
М141БПЛТЛ04657747585.07575.084.0570
М141БПЛТЛ04758647595.06464.074.0880
М141БПЛТЛ06355648444.05454.075.0880
М141БПЛТЛ02968879567.06585.074.0570
М141БПЛТЛ06478676684.06444.065.0470
М141БПЛТЛ07677868666.08685.074.0460
М141БПЛТЛ06277769665.06564.055.0460
М141БПЛТЛ07456747656.06686.066.0881
13023203867658484.08455.064.0560
М141БПЛТЛ02379689694.07776.044.0751
М141БПЛТЛ05478648644.06484.044.0481
М141БПЛТЛ012667410654.07574.054.0481
М141БПЛТЛ00665658555.06475.075.0680
М141БПЛТЛ05565647748.05464.065.0451
М141БПЛТЛ00767767674.05565.045.0471
М141БПЛТЛ05086668454.05564.054.0660
М141БПЛТЛ066710779584.06564.064.0560
М141БПЛТЛ04355658565.06454.05NaN460
М141БПЛТЛ0846784855NaN8444.044.0671
М141БПЛТЛ00557557474.05455.044.0481
М141БПЛТЛ04445746445.04444.06NaN551
1305103854449555.0544NaN74.0441
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ024 7 9 8 8 9 8 10 8.0 7 9 \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 10 \n", "М141БПЛТЛ075 9 9 9 10 9 10 9 8.0 9 10 \n", "М141БПЛТЛ017 9 9 8 8 9 9 10 6.0 9 9 \n", "М141БПЛТЛ069 10 10 10 10 10 10 9 8.0 8 10 \n", "М141БПЛТЛ072 10 9 8 10 9 8 9 8.0 8 10 \n", "М141БПЛТЛ020 8 7 7 6 9 10 8 8.0 7 7 \n", "М141БПЛТЛ026 7 10 8 7 10 7 9 8.0 8 8 \n", "М141БПЛТЛ073 7 9 8 8 9 8 9 8.0 8 9 \n", "М141БПЛТЛ078 6 6 9 5 6 10 7 6.0 8 6 \n", "М141БПЛТЛ060 7 8 7 7 9 8 8 5.0 7 5 \n", "М141БПЛТЛ040 6 9 8 6 9 7 8 6.0 9 5 \n", "М141БПЛТЛ065 9 9 8 4 8 8 7 9.0 8 5 \n", "М141БПЛТЛ053 6 7 7 5 9 8 7 8.0 8 6 \n", "М141БПЛТЛ015 6 9 7 6 9 7 9 4.0 7 7 \n", "М141БПЛТЛ021 8 9 8 8 9 8 8 7.0 7 7 \n", "М141БПЛТЛ018 7 7 9 7 9 7 8 6.0 6 7 \n", "М141БПЛТЛ039 9 8 9 8 8 8 6 8.0 7 6 \n", "М141БПЛТЛ036 8 10 7 8 8 6 9 4.0 8 8 \n", "М141БПЛТЛ049 6 7 6 6 8 6 8 4.0 8 5 \n", "06114043 8 8 10 5 8 8 8 10.0 7 7 \n", "М141БПЛТЛ048 8 6 8 6 9 6 4 4.0 6 4 \n", "М141БПЛТЛ034 6 9 7 6 9 6 8 6.0 7 6 \n", "М141БПЛТЛ045 5 8 8 7 8 6 7 6.0 7 7 \n", "М141БПЛТЛ033 5 9 8 7 9 7 9 7.0 7 8 \n", "М141БПЛТЛ083 5 5 6 5 8 7 6 5.0 7 5 \n", "М141БПЛТЛ008 10 8 8 9 8 10 9 8.0 9 10 \n", "М141БПЛТЛ001 6 7 7 4 10 7 7 6.0 8 6 \n", "М141БПЛТЛ038 7 9 6 4 9 6 7 6.0 7 4 \n", "М141БПЛТЛ052 7 7 7 7 8 6 6 6.0 8 6 \n", "М141БПЛТЛ011 7 6 8 6 9 6 6 5.0 6 6 \n", "М141БПЛТЛ004 7 7 6 6 8 6 6 5.0 5 5 \n", "М141БПЛТЛ010 6 6 7 6 9 7 7 6.0 7 5 \n", "М141БПЛТЛ071 6 9 7 7 9 6 8 4.0 6 7 \n", "М141БПЛТЛ035 5 6 7 6 8 5 5 4.0 6 6 \n", "М141БПЛТЛ030 7 6 6 6 7 6 6 4.0 8 5 \n", "М141БПЛТЛ070 5 5 6 4 8 6 5 5.0 6 4 \n", "М141БПЛТЛ051 8 9 8 6 8 7 6 7.0 6 6 \n", "М141БПЛТЛ046 5 7 7 4 7 5 8 5.0 7 5 \n", "М141БПЛТЛ047 5 8 6 4 7 5 9 5.0 6 4 \n", "М141БПЛТЛ063 5 5 6 4 8 4 4 4.0 5 4 \n", "М141БПЛТЛ029 6 8 8 7 9 5 6 7.0 6 5 \n", "М141БПЛТЛ064 7 8 6 7 6 6 8 4.0 6 4 \n", "М141БПЛТЛ076 7 7 8 6 8 6 6 6.0 8 6 \n", "М141БПЛТЛ062 7 7 7 6 9 6 6 5.0 6 5 \n", "М141БПЛТЛ074 5 6 7 4 7 6 5 6.0 6 6 \n", "130232038 6 7 6 5 8 4 8 4.0 8 4 \n", "М141БПЛТЛ023 7 9 6 8 9 6 9 4.0 7 7 \n", "М141БПЛТЛ054 7 8 6 4 8 6 4 4.0 6 4 \n", "М141БПЛТЛ012 6 6 7 4 10 6 5 4.0 7 5 \n", "М141БПЛТЛ006 6 5 6 5 8 5 5 5.0 6 4 \n", "М141БПЛТЛ055 6 5 6 4 7 7 4 8.0 5 4 \n", "М141БПЛТЛ007 6 7 7 6 7 6 7 4.0 5 5 \n", "М141БПЛТЛ050 8 6 6 6 8 4 5 4.0 5 5 \n", "М141БПЛТЛ066 7 10 7 7 9 5 8 4.0 6 5 \n", "М141БПЛТЛ043 5 5 6 5 8 5 6 5.0 6 4 \n", "М141БПЛТЛ084 6 7 8 4 8 5 5 NaN 8 4 \n", "М141БПЛТЛ005 5 7 5 5 7 4 7 4.0 5 4 \n", "М141БПЛТЛ044 4 5 7 4 6 4 4 5.0 4 4 \n", "13051038 5 4 4 4 9 5 5 5.0 5 4 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ024 9 7.0 8 8.0 6 10 1 \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 \n", "М141БПЛТЛ075 9 9.0 8 8.0 7 9 1 \n", "М141БПЛТЛ017 9 8.0 8 8.0 8 9 0 \n", "М141БПЛТЛ069 9 7.0 6 5.0 8 10 1 \n", "М141БПЛТЛ072 9 7.0 8 8.0 9 9 0 \n", "М141БПЛТЛ020 9 7.0 8 6.0 8 9 1 \n", "М141БПЛТЛ026 8 8.0 8 7.0 7 8 0 \n", "М141БПЛТЛ073 9 7.0 7 6.0 10 9 1 \n", "М141БПЛТЛ078 9 6.0 8 8.0 6 7 0 \n", "М141БПЛТЛ060 8 5.0 7 8.0 7 9 1 \n", "М141БПЛТЛ040 8 5.0 8 5.0 7 10 0 \n", "М141БПЛТЛ065 10 9.0 8 8.0 6 9 1 \n", "М141БПЛТЛ053 8 7.0 8 6.0 9 9 0 \n", "М141БПЛТЛ015 7 6.0 7 7.0 10 7 0 \n", "М141БПЛТЛ021 6 6.0 8 6.0 7 8 0 \n", "М141БПЛТЛ018 8 7.0 7 7.0 7 8 0 \n", "М141БПЛТЛ039 9 6.0 7 8.0 4 9 1 \n", "М141БПЛТЛ036 7 6.0 7 6.0 7 8 1 \n", "М141БПЛТЛ049 9 6.0 8 5.0 6 8 0 \n", "06114043 9 NaN 7 8.0 7 8 1 \n", "М141БПЛТЛ048 8 4.0 6 7.0 7 8 0 \n", "М141БПЛТЛ034 6 5.0 8 5.0 8 9 0 \n", "М141БПЛТЛ045 8 6.0 8 6.0 5 8 0 \n", "М141БПЛТЛ033 8 7.0 8 5.0 7 8 0 \n", "М141БПЛТЛ083 7 5.0 7 5.0 4 7 0 \n", "М141БПЛТЛ008 9 8.0 5 5.0 10 4 1 \n", "М141БПЛТЛ001 8 4.0 6 6.0 4 8 0 \n", "М141БПЛТЛ038 8 4.0 5 4.0 9 7 1 \n", "М141БПЛТЛ052 7 5.0 8 6.0 5 7 1 \n", "М141БПЛТЛ011 7 6.0 8 6.0 5 8 0 \n", "М141БПЛТЛ004 6 5.0 7 5.0 8 8 0 \n", "М141БПЛТЛ010 8 6.0 8 6.0 5 8 1 \n", "М141БПЛТЛ071 7 6.0 5 NaN 5 7 0 \n", "М141БПЛТЛ035 7 5.0 8 7.0 6 7 0 \n", "М141БПЛТЛ030 5 5.0 8 5.0 7 9 1 \n", "М141БПЛТЛ070 5 6.0 8 5.0 6 7 0 \n", "М141БПЛТЛ051 6 5.0 4 4.0 5 5 1 \n", "М141БПЛТЛ046 7 5.0 8 4.0 5 7 0 \n", "М141БПЛТЛ047 6 4.0 7 4.0 8 8 0 \n", "М141БПЛТЛ063 5 4.0 7 5.0 8 8 0 \n", "М141БПЛТЛ029 8 5.0 7 4.0 5 7 0 \n", "М141БПЛТЛ064 4 4.0 6 5.0 4 7 0 \n", "М141БПЛТЛ076 8 5.0 7 4.0 4 6 0 \n", "М141БПЛТЛ062 6 4.0 5 5.0 4 6 0 \n", "М141БПЛТЛ074 8 6.0 6 6.0 8 8 1 \n", "130232038 5 5.0 6 4.0 5 6 0 \n", "М141БПЛТЛ023 7 6.0 4 4.0 7 5 1 \n", "М141БПЛТЛ054 8 4.0 4 4.0 4 8 1 \n", "М141БПЛТЛ012 7 4.0 5 4.0 4 8 1 \n", "М141БПЛТЛ006 7 5.0 7 5.0 6 8 0 \n", "М141БПЛТЛ055 6 4.0 6 5.0 4 5 1 \n", "М141БПЛТЛ007 6 5.0 4 5.0 4 7 1 \n", "М141БПЛТЛ050 6 4.0 5 4.0 6 6 0 \n", "М141БПЛТЛ066 6 4.0 6 4.0 5 6 0 \n", "М141БПЛТЛ043 5 4.0 5 NaN 4 6 0 \n", "М141БПЛТЛ084 4 4.0 4 4.0 6 7 1 \n", "М141БПЛТЛ005 5 5.0 4 4.0 4 8 1 \n", "М141БПЛТЛ044 4 4.0 6 NaN 5 5 1 \n", "13051038 4 NaN 7 4.0 4 4 1 " ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[(df[\"Eng\"]) > 9 & (df[\"Law\"] < 9)] # первая закрывающая скобка не после 9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Результат получился совсем неверным. Потому что Python понял наше условие не так, как нужно. Теперь выберем студентов с оценкой по политической истории ниже 5 или с оценкой по истории политических учений ниже 5:" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMale
id
М141БПЛТЛ01569769794.07776.077.01070
М141БПЛТЛ036810788694.08876.076.0781
М141БПЛТЛ04967668684.08596.085.0680
М141БПЛТЛ04886869644.06484.067.0780
М141БПЛТЛ07169779684.06776.05NaN570
М141БПЛТЛ03556768554.06675.087.0670
М141БПЛТЛ03076667664.08555.085.0791
М141БПЛТЛ06355648444.05454.075.0880
М141БПЛТЛ06478676684.06444.065.0470
13023203867658484.08455.064.0560
М141БПЛТЛ02379689694.07776.044.0751
М141БПЛТЛ05478648644.06484.044.0481
М141БПЛТЛ012667410654.07574.054.0481
М141БПЛТЛ00767767674.05565.045.0471
М141БПЛТЛ05086668454.05564.054.0660
М141БПЛТЛ066710779584.06564.064.0560
М141БПЛТЛ00557557474.05455.044.0481
М141БПЛТЛ04445746445.04444.06NaN551
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ015 6 9 7 6 9 7 9 4.0 7 7 \n", "М141БПЛТЛ036 8 10 7 8 8 6 9 4.0 8 8 \n", "М141БПЛТЛ049 6 7 6 6 8 6 8 4.0 8 5 \n", "М141БПЛТЛ048 8 6 8 6 9 6 4 4.0 6 4 \n", "М141БПЛТЛ071 6 9 7 7 9 6 8 4.0 6 7 \n", "М141БПЛТЛ035 5 6 7 6 8 5 5 4.0 6 6 \n", "М141БПЛТЛ030 7 6 6 6 7 6 6 4.0 8 5 \n", "М141БПЛТЛ063 5 5 6 4 8 4 4 4.0 5 4 \n", "М141БПЛТЛ064 7 8 6 7 6 6 8 4.0 6 4 \n", "130232038 6 7 6 5 8 4 8 4.0 8 4 \n", "М141БПЛТЛ023 7 9 6 8 9 6 9 4.0 7 7 \n", "М141БПЛТЛ054 7 8 6 4 8 6 4 4.0 6 4 \n", "М141БПЛТЛ012 6 6 7 4 10 6 5 4.0 7 5 \n", "М141БПЛТЛ007 6 7 7 6 7 6 7 4.0 5 5 \n", "М141БПЛТЛ050 8 6 6 6 8 4 5 4.0 5 5 \n", "М141БПЛТЛ066 7 10 7 7 9 5 8 4.0 6 5 \n", "М141БПЛТЛ005 5 7 5 5 7 4 7 4.0 5 4 \n", "М141БПЛТЛ044 4 5 7 4 6 4 4 5.0 4 4 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male \n", "id \n", "М141БПЛТЛ015 7 6.0 7 7.0 10 7 0 \n", "М141БПЛТЛ036 7 6.0 7 6.0 7 8 1 \n", "М141БПЛТЛ049 9 6.0 8 5.0 6 8 0 \n", "М141БПЛТЛ048 8 4.0 6 7.0 7 8 0 \n", "М141БПЛТЛ071 7 6.0 5 NaN 5 7 0 \n", "М141БПЛТЛ035 7 5.0 8 7.0 6 7 0 \n", "М141БПЛТЛ030 5 5.0 8 5.0 7 9 1 \n", "М141БПЛТЛ063 5 4.0 7 5.0 8 8 0 \n", "М141БПЛТЛ064 4 4.0 6 5.0 4 7 0 \n", "130232038 5 5.0 6 4.0 5 6 0 \n", "М141БПЛТЛ023 7 6.0 4 4.0 7 5 1 \n", "М141БПЛТЛ054 8 4.0 4 4.0 4 8 1 \n", "М141БПЛТЛ012 7 4.0 5 4.0 4 8 1 \n", "М141БПЛТЛ007 6 5.0 4 5.0 4 7 1 \n", "М141БПЛТЛ050 6 4.0 5 4.0 6 6 0 \n", "М141БПЛТЛ066 6 4.0 6 4.0 5 6 0 \n", "М141БПЛТЛ005 5 5.0 4 4.0 4 8 1 \n", "М141БПЛТЛ044 4 4.0 6 NaN 5 5 1 " ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[(df[\"Phist\"] < 5) | (df[\"Polth\"] < 5)] # оператор | для условия или " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Здесь наше выражение в квадратных скобках принимает значение *True*, когда хотя бы одно из условий верно: либо верно первое, либо второе, либо оба." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Добавление новых столбцов в таблице и удаление пропущенных значений" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Давайте добавим в нашу таблицу `df` новый столбец, который будет представлять собой среднюю оценку по социологии (посчитаем среднее арифметическое оценок по социологии и политической социологии). Чтобы добавить новый столбец, нужно указать его название в квадратных скобках:" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "df[\"Avg_Soc\"] = (df[\"Soc\"] + df[\"Polsoc\"]) / 2" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMaleAvg_Soc
id
М141БПЛТЛ024798898108.07997.088.061018.5
М141БПЛТЛ03181010101010109.0910109.088.0910110.0
М141БПЛТЛ0759991091098.091099.088.07919.0
М141БПЛТЛ017998899106.09998.088.08908.5
М141БПЛТЛ06910101010101098.081097.065.081019.5
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ024 7 9 8 8 9 8 10 8.0 7 9 \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9.0 9 10 \n", "М141БПЛТЛ075 9 9 9 10 9 10 9 8.0 9 10 \n", "М141БПЛТЛ017 9 9 8 8 9 9 10 6.0 9 9 \n", "М141БПЛТЛ069 10 10 10 10 10 10 9 8.0 8 10 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male Avg_Soc \n", "id \n", "М141БПЛТЛ024 9 7.0 8 8.0 6 10 1 8.5 \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 10.0 \n", "М141БПЛТЛ075 9 9.0 8 8.0 7 9 1 9.0 \n", "М141БПЛТЛ017 9 8.0 8 8.0 8 9 0 8.5 \n", "М141БПЛТЛ069 9 7.0 6 5.0 8 10 1 9.5 " ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Теперь внесем изменения в уже существующий столбец в таблице. В самом начале мы заметили, что некоторые столбцы имеют тип `float` (числа с плавающей точкой), а не `integer` (целые числа). Давайте попробуем сделать столбец с политической историей целочисленным." ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "cannot convert float NaN to integer", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnewh\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"Phist\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnewh\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"Phist\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: cannot convert float NaN to integer" ] } ], "source": [ "newh = [int(i) for i in df[\"Phist\"]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Не получается! Почему? Python пишет, что не может превратить *NaN* в *integer*. Действительно, сложно превратить объект *Not a number* в целое число. Тип *float* относится к нему толерантно, а вот тип *integer* уже нет. Как быть? Давайте просто удалим из датафрейма все пропущенные значения (то есть строки, содержащие пропущенные значения). " ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "df = df.dropna() # удаляем и сохраняем изменения" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Теперь проделаем те же операции:" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "newh = [int(i) for i in df[\"Phist\"]]" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", " \"\"\"Entry point for launching an IPython kernel.\n" ] } ], "source": [ "df[\"Phist\"] = newh" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Получилось!" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpsMstatSocEconEngPolthMstat2PhistLawPhilPolsocPtheoPregComppGameWpolMaleAvg_Soc
id
М141БПЛТЛ0247988981087997.088.061018.5
М141БПЛТЛ03181010101010109910109.088.0910110.0
М141БПЛТЛ075999109109891099.088.07919.0
М141БПЛТЛ0179988991069998.088.08908.5
М141БПЛТЛ0691010101010109881097.065.081019.5
\n", "
" ], "text/plain": [ " Cps Mstat Soc Econ Eng Polth Mstat2 Phist Law Phil \\\n", "id \n", "М141БПЛТЛ024 7 9 8 8 9 8 10 8 7 9 \n", "М141БПЛТЛ031 8 10 10 10 10 10 10 9 9 10 \n", "М141БПЛТЛ075 9 9 9 10 9 10 9 8 9 10 \n", "М141БПЛТЛ017 9 9 8 8 9 9 10 6 9 9 \n", "М141БПЛТЛ069 10 10 10 10 10 10 9 8 8 10 \n", "\n", " Polsoc Ptheo Preg Compp Game Wpol Male Avg_Soc \n", "id \n", "М141БПЛТЛ024 9 7.0 8 8.0 6 10 1 8.5 \n", "М141БПЛТЛ031 10 9.0 8 8.0 9 10 1 10.0 \n", "М141БПЛТЛ075 9 9.0 8 8.0 7 9 1 9.0 \n", "М141БПЛТЛ017 9 8.0 8 8.0 8 9 0 8.5 \n", "М141БПЛТЛ069 9 7.0 6 5.0 8 10 1 9.5 " ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head() # Phist уже с целыми значениями" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Еще немного про описательные статистики" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "В самом начале мы обсуждали описание базы данных с помощью метода `.describe()`. Помимо этого метода существует много методов, которые выводят отдельные статистики." ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Cps 7.00\n", "Mstat 7.50\n", "Soc 7.00\n", "Econ 6.00\n", "Eng 8.50\n", "Polth 6.00\n", "Mstat2 7.00\n", "Phist 6.00\n", "Law 7.00\n", "Phil 6.00\n", "Polsoc 8.00\n", "Ptheo 5.00\n", "Preg 7.00\n", "Compp 5.00\n", "Game 6.00\n", "Wpol 8.00\n", "Male 0.00\n", "Avg_Soc 7.25\n", "dtype: float64" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.median() # медиана (для всех показателей)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Можно запрашивать статистики по отдельным переменным (столбцам):" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.833333333333333" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.Phist.mean() # среднее арифметическое" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Или по наблюдениям (строкам):" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.25" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[\"М141БПЛТЛ023\"].mean() " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Давайте теперь построим какие-нибудь графики. Библиотеку `pandas` удобно использовать в сочетании с библиотекой для построения графиков matplotlib. Давайте её импортируем (эта библиотека должна была быть установлена на ваш компьютер вместе с Anaconda)." ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "import matplotlib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Теперь добавим элементы магии :) Магическую строку в Jupyter Notebook (*Python magic*). Эта строка позволит выводить графики прямо внутри ноутбука, файла `.ipynb`, а не в отдельном окне. " ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Построим гистограмму для оценок по теории игр. " ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAD8CAYAAAB6paOMAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0R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NFwFwDrC9dYgV8pSqetwaOCXurcAlVfUo4LGs0u+nqr4+fB+PAx4P3AZ8rHGsA5bkSODlwFJVHc/kBI6z2qaaTZLjgRczuXvBY4FnJjm2baoD8h7gafss2wxcXlXHApcP8ytuzRZBkqOA04HzW2fRRJIHACcD7wSoqjuq6r/bploRpwL/UVXfbh1kRuuBQ5OsBzZwD9f1rBKPBv6tqm6rqruALwLPaZxpalX1JeAH+yw+E7hgmL4AePY8xl6zRQC8BXgV8LPWQVZAAZcm2TZccb1aHQPsBt497LI7P8lhrUOtgLOAC1uHmEVVfQd4I3AjcAvwP1V1adtUM7sWODnJQ5JsAJ7B3S9eXY0eWlW3AAyvR8xjkDVZBEmeCeyqqm2ts6yQk6rqRCZ3bH1pkpNbB5rReuBE4G1VdQLwY+a0qTuW4aLIM4CLWmeZxbDP+UzgaODhwGFJXtA21WyqajvwBuAy4BLg34G7moZaJdZkEQAnAWck2cHk7qanJHl/20izq6qbh9ddTPZDP7FtopntBHZW1RXD/MVMimE1ezpwZVV9r3WQGT0V+FZV7a6qO4GPAk9unGlmVfXOqjqxqk5mspvlhtaZlul7SR4GMLzumscga7IIquo1VXVUVS0y2Wz/fFWtyv/LSXJYkvvvmQZ+j8km8KpTVd8Fbkpy3LDoVOD6hpFWwvNZpbuFBjcCT0qyIUmYfCer8gA+QJIjhteNwHNZ3d8NTG6/c/YwfTbwiXkM0uTKYh2QhwIfm/wbZT3wwaq6pG2kZXkZ8IFhl8o3gRc2zjOzYT/0acBLWmeZVVVdkeRi4Eomu1GuYnVflfuRJA8B7gReWlX/1TrQtJJcCPwucHiSncC5wHnAh5O8iElpP28uY3tlsST1bU3uGpIkTc8ikKTOWQSS1DmLQJI6ZxFIUucsAknqnEUgSZ2zCCSpc/8HBCSFFd5RE6UAAAAASUVORK5CYII=\n", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df[\"Game\"].plot.hist() # histogram" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Что показывает этот график? Он показывает, сколько студентов получили те или иные оценки. По гистограмме видно, что больше всего по этому курсу оценок 4 и 7. \n", "\n", "Можно поменять цвет гистограммы:" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAD8CAYAAAB6paOMAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADuFJREFUeJzt3XusZWV9xvHv44wGhmpROVgFx4GGoIZUwaOx0tIK0ngFNbXF1IZa65jUKtomisYU/2liE+ulaWMd8YI3jOC11iKIVdPEojNAw2U0WEUYQWesbVGhAvrrH3tNHcaR2bPP2euds9/vJznZe61Zs99nzZ7hYd1TVUiS+nWf1gEkSW1ZBJLUOYtAkjpnEUhS5ywCSeqcRSBJnbMIJKlzFoEkdc4ikKTOrW8dYBpHHHFEbdq0qXUMSVpTtm3b9r2qWtrfcmuiCDZt2sTWrVtbx5CkNSXJt6ZZzl1DktQ5i0CSOmcRSFLnLAJJ6pxFIEmdswgkqXMWgSR1ziKQpM5ZBJLUuTVxZfGKJG3GrWozrsbV6u8X+HdMq8YtAknqnEUgSZ2zCCSpcxaBJHXOIpCkzlkEktQ5i0CSOmcRSFLnLAJJ6pxFIEmdswgkqXMWgSR1ziKQpM5ZBJLUubkVQZJ3JdmZ5No95j0oyWVJbhheHziv8SVJ05nnFsF7gKfuNe9c4PKqOg64fJiWJDU0tyKoqi8C399r9pnABcP7C4Bnz2t8SdJ0xj5G8JCquhVgeD1y5PElSXs5aA8WJ9mcZGuSrbt27WodR5IW1thF8N0kDwUYXnf+ogWraktVLVfV8tLS0mgBJak3YxfBJ4Gzh/dnA58YeXxJ0l7mefrohcC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zKqqrkhyMXAlk90oV7G2r8r9SJIHA3cBL62q/2odaFpJLgR+GzgiyQ7gPOANwIeTvIhJaT9vLmN7ZbEk9W0hdw1JkqZnEUhS5ywCSeqcRSBJnbMIJKlzFoEkdc4ikKTOWQSS1Ln/A972e/WEttqAAAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df[\"Game\"].plot.hist(color = \"red\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Можно пытаться строить другие графики. Например, построить ящик с усами." ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAACyxJREFUeJzt3X+s3Xddx/HnayuElcDE9W4xzssVMxcUMiInxDGdwDaijvBD/WNEzDTGGw3Cxj9S/zDT/0pCoos/Ym7mjyXIjAxQw5LZBQLThA1uB7iOogujxUFdu0zHlOE29vaPnsZr06493+/39nbvPh/JyT3n3O+Pd//os9987vn2pqqQJD3/nbPVA0iSpmHQJakJgy5JTRh0SWrCoEtSEwZdkpow6JLUhEGXpCYMuiQ1se10nmzHjh21srJyOk8pSc97e/bsebSqlk623WkN+srKCuvr66fzlJL0vJfkwKls55KLJDVh0CWpCYMuSU0YdElqwqBLUhMnDXqSP09yKMneDe99b5K7kjw4//qyzR1TknQyp3KF/pfATx/z3k7gk1V1CfDJ+WtJ0hY6adCr6m7gsWPefhtw6/z5rcDbJ55LkrSgoTcWXVRVBwGq6mCSC0+0YZJVYBVgeXl54OmkxSQ5Lefxd/LqTLLpPxStqrWqmlXVbGnpp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zky8CvTzi6NJp3ikpSE16hS1ITBl2SmjDoktSEQZekJgy6JDVh0CWpCYMuSU0YdElq4n8BVwS7Yzkt8nwAAAAASUVORK5CYII=\n", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df[\"Game\"].plot.box() # boxplot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Этот график визуализирует основные описательные статистики переменной и отображает форму её распределения. Нижняя граница яшика – это нижний квартиль, верхняя – верхний квартиль, линяя внутри ящика – медиана. Усы графика могут откладываться по-разному: если в переменной встречаются нетипичные значения (выбросы), то границы усов совпадают с границами типичных значений, если нетипичных значений нет, границы усов соответствуют минимальному и максимальному значению переменной. Подробнее про ящик с усами см. [здесь](https://ru.wikipedia.org/wiki/%D0%AF%D1%89%D0%B8%D0%BA_%D1%81_%D1%83%D1%81%D0%B0%D0%BC%D0%B8)." ] } ], "metadata": { "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }