{
"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",
" id | \n",
" catps | \n",
" mstat | \n",
" soc | \n",
" econ | \n",
" eng | \n",
" polth | \n",
" mstat2 | \n",
" phist | \n",
" law | \n",
" phil | \n",
" polsoc | \n",
" ptheo | \n",
" preg | \n",
" compp | \n",
" game | \n",
" wpol | \n",
" male | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" М141БПЛТЛ024 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" 1 | \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" 2 | \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" 3 | \n",
" М141БПЛТЛ017 | \n",
" 9 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 6.0 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" 4 | \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
"
\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",
" id | \n",
" catps | \n",
" mstat | \n",
" soc | \n",
" econ | \n",
" eng | \n",
" polth | \n",
" mstat2 | \n",
" phist | \n",
" law | \n",
" phil | \n",
" polsoc | \n",
" ptheo | \n",
" preg | \n",
" compp | \n",
" game | \n",
" wpol | \n",
" male | \n",
"
\n",
" \n",
" \n",
" \n",
" 55 | \n",
" М141БПЛТЛ043 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 5 | \n",
" 8 | \n",
" 5 | \n",
" 6 | \n",
" 5.0 | \n",
" 6 | \n",
" 4 | \n",
" 5 | \n",
" 4.0 | \n",
" 5 | \n",
" NaN | \n",
" 4 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" 56 | \n",
" М141БПЛТЛ084 | \n",
" 6 | \n",
" 7 | \n",
" 8 | \n",
" 4 | \n",
" 8 | \n",
" 5 | \n",
" 5 | \n",
" NaN | \n",
" 8 | \n",
" 4 | \n",
" 4 | \n",
" 4.0 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" 7 | \n",
" 1 | \n",
"
\n",
" \n",
" 57 | \n",
" М141БПЛТЛ005 | \n",
" 5 | \n",
" 7 | \n",
" 5 | \n",
" 5 | \n",
" 7 | \n",
" 4 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
" 4 | \n",
" 5 | \n",
" 5.0 | \n",
" 4 | \n",
" 4.0 | \n",
" 4 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" 58 | \n",
" М141БПЛТЛ044 | \n",
" 4 | \n",
" 5 | \n",
" 7 | \n",
" 4 | \n",
" 6 | \n",
" 4 | \n",
" 4 | \n",
" 5.0 | \n",
" 4 | \n",
" 4 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" NaN | \n",
" 5 | \n",
" 5 | \n",
" 1 | \n",
"
\n",
" \n",
" 59 | \n",
" 13051038 | \n",
" 5 | \n",
" 4 | \n",
" 4 | \n",
" 4 | \n",
" 9 | \n",
" 5 | \n",
" 5 | \n",
" 5.0 | \n",
" 5 | \n",
" 4 | \n",
" 4 | \n",
" NaN | \n",
" 7 | \n",
" 4.0 | \n",
" 4 | \n",
" 4 | \n",
" 1 | \n",
"
\n",
" \n",
"
\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",
" id | \n",
" catps | \n",
" mstat | \n",
" soc | \n",
" econ | \n",
" eng | \n",
" polth | \n",
" mstat2 | \n",
" phist | \n",
" law | \n",
" phil | \n",
" polsoc | \n",
" ptheo | \n",
" preg | \n",
" compp | \n",
" game | \n",
" wpol | \n",
" male | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" М141БПЛТЛ024 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" 1 | \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" 2 | \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" 3 | \n",
" М141БПЛТЛ017 | \n",
" 9 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 6.0 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" 4 | \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" 5 | \n",
" М141БПЛТЛ072 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" 6 | \n",
" М141БПЛТЛ020 | \n",
" 8 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 10 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 8 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" 7 | \n",
" М141БПЛТЛ026 | \n",
" 7 | \n",
" 10 | \n",
" 8 | \n",
" 7 | \n",
" 10 | \n",
" 7 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" 8 | \n",
" М141БПЛТЛ073 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 7 | \n",
" 6.0 | \n",
" 10 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" 9 | \n",
" М141БПЛТЛ078 | \n",
" 6 | \n",
" 6 | \n",
" 9 | \n",
" 5 | \n",
" 6 | \n",
" 10 | \n",
" 7 | \n",
" 6.0 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 6.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
"
\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",
" catps | \n",
" mstat | \n",
" soc | \n",
" econ | \n",
" eng | \n",
" polth | \n",
" mstat2 | \n",
" phist | \n",
" law | \n",
" phil | \n",
" polsoc | \n",
" ptheo | \n",
" preg | \n",
" compp | \n",
" game | \n",
" wpol | \n",
" male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ024 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ017 | \n",
" 9 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 6.0 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ024 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ017 | \n",
" 9 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 6.0 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ024 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ017 | \n",
" 9 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 6.0 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
"
\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",
" Soc | \n",
" Polsoc | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ024 | \n",
" 8 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ031 | \n",
" 10 | \n",
" 10 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ017 | \n",
" 8 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ072 | \n",
" 8 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ020 | \n",
" 7 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ026 | \n",
" 8 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ073 | \n",
" 8 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ078 | \n",
" 9 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ060 | \n",
" 7 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ040 | \n",
" 8 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ065 | \n",
" 8 | \n",
" 10 | \n",
"
\n",
" \n",
" М141БПЛТЛ053 | \n",
" 7 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ015 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ021 | \n",
" 8 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ018 | \n",
" 9 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ039 | \n",
" 9 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ036 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ049 | \n",
" 6 | \n",
" 9 | \n",
"
\n",
" \n",
" 06114043 | \n",
" 10 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ048 | \n",
" 8 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ034 | \n",
" 7 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ045 | \n",
" 8 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ033 | \n",
" 8 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ083 | \n",
" 6 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ008 | \n",
" 8 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ001 | \n",
" 7 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ038 | \n",
" 6 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ052 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ011 | \n",
" 8 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ004 | \n",
" 6 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ010 | \n",
" 7 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ071 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ035 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ030 | \n",
" 6 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ070 | \n",
" 6 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ051 | \n",
" 8 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ046 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ047 | \n",
" 6 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ063 | \n",
" 6 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ029 | \n",
" 8 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ064 | \n",
" 6 | \n",
" 4 | \n",
"
\n",
" \n",
" М141БПЛТЛ076 | \n",
" 8 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ062 | \n",
" 7 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ074 | \n",
" 7 | \n",
" 8 | \n",
"
\n",
" \n",
" 130232038 | \n",
" 6 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ023 | \n",
" 6 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ054 | \n",
" 6 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ012 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ006 | \n",
" 6 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ055 | \n",
" 6 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ007 | \n",
" 7 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ050 | \n",
" 6 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ066 | \n",
" 7 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ043 | \n",
" 6 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ084 | \n",
" 8 | \n",
" 4 | \n",
"
\n",
" \n",
" М141БПЛТЛ005 | \n",
" 5 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ044 | \n",
" 7 | \n",
" 4 | \n",
"
\n",
" \n",
" 13051038 | \n",
" 4 | \n",
" 4 | \n",
"
\n",
" \n",
"
\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",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ024 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 8.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ031 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ017 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 6.0 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ072 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ020 | \n",
" 6 | \n",
" 9 | \n",
" 10 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ026 | \n",
" 7 | \n",
" 10 | \n",
" 7 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ073 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ078 | \n",
" 5 | \n",
" 6 | \n",
" 10 | \n",
" 7 | \n",
" 6.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ060 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ040 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 8 | \n",
" 6.0 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ065 | \n",
" 4 | \n",
" 8 | \n",
" 8 | \n",
" 7 | \n",
" 9.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ053 | \n",
" 5 | \n",
" 9 | \n",
" 8 | \n",
" 7 | \n",
" 8.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ015 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 9 | \n",
" 4.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ021 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 7.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ018 | \n",
" 7 | \n",
" 9 | \n",
" 7 | \n",
" 8 | \n",
" 6.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ039 | \n",
" 8 | \n",
" 8 | \n",
" 8 | \n",
" 6 | \n",
" 8.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ036 | \n",
" 8 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 4.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ049 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 8 | \n",
"
\n",
" \n",
" 06114043 | \n",
" 5 | \n",
" 8 | \n",
" 8 | \n",
" 8 | \n",
" 10.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ048 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ034 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 8 | \n",
" 6.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ045 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ033 | \n",
" 7 | \n",
" 9 | \n",
" 7 | \n",
" 9 | \n",
" 7.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ083 | \n",
" 5 | \n",
" 8 | \n",
" 7 | \n",
" 6 | \n",
" 5.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ008 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ001 | \n",
" 4 | \n",
" 10 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ038 | \n",
" 4 | \n",
" 9 | \n",
" 6 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ052 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 6.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ011 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ004 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ010 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ071 | \n",
" 7 | \n",
" 9 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ035 | \n",
" 6 | \n",
" 8 | \n",
" 5 | \n",
" 5 | \n",
" 4.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ030 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 4.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ070 | \n",
" 4 | \n",
" 8 | \n",
" 6 | \n",
" 5 | \n",
" 5.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ051 | \n",
" 6 | \n",
" 8 | \n",
" 7 | \n",
" 6 | \n",
" 7.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ046 | \n",
" 4 | \n",
" 7 | \n",
" 5 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ047 | \n",
" 4 | \n",
" 7 | \n",
" 5 | \n",
" 9 | \n",
" 5.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ063 | \n",
" 4 | \n",
" 8 | \n",
" 4 | \n",
" 4 | \n",
" 4.0 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ029 | \n",
" 7 | \n",
" 9 | \n",
" 5 | \n",
" 6 | \n",
" 7.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ064 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ076 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 6.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ062 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ074 | \n",
" 4 | \n",
" 7 | \n",
" 6 | \n",
" 5 | \n",
" 6.0 | \n",
" 6 | \n",
"
\n",
" \n",
" 130232038 | \n",
" 5 | \n",
" 8 | \n",
" 4 | \n",
" 8 | \n",
" 4.0 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ023 | \n",
" 8 | \n",
" 9 | \n",
" 6 | \n",
" 9 | \n",
" 4.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ054 | \n",
" 4 | \n",
" 8 | \n",
" 6 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ012 | \n",
" 4 | \n",
" 10 | \n",
" 6 | \n",
" 5 | \n",
" 4.0 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ006 | \n",
" 5 | \n",
" 8 | \n",
" 5 | \n",
" 5 | \n",
" 5.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ055 | \n",
" 4 | \n",
" 7 | \n",
" 7 | \n",
" 4 | \n",
" 8.0 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ007 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ050 | \n",
" 6 | \n",
" 8 | \n",
" 4 | \n",
" 5 | \n",
" 4.0 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ066 | \n",
" 7 | \n",
" 9 | \n",
" 5 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ043 | \n",
" 5 | \n",
" 8 | \n",
" 5 | \n",
" 6 | \n",
" 5.0 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ084 | \n",
" 4 | \n",
" 8 | \n",
" 5 | \n",
" 5 | \n",
" NaN | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ005 | \n",
" 5 | \n",
" 7 | \n",
" 4 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ044 | \n",
" 4 | \n",
" 6 | \n",
" 4 | \n",
" 4 | \n",
" 5.0 | \n",
" 4 | \n",
"
\n",
" \n",
" 13051038 | \n",
" 4 | \n",
" 9 | \n",
" 5 | \n",
" 5 | \n",
" 5.0 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" Mstat | \n",
" Soc | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ024 | \n",
" 9 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ031 | \n",
" 10 | \n",
" 10 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ017 | \n",
" 9 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
"
\n",
" \n",
" М141БПЛТЛ072 | \n",
" 9 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ020 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ026 | \n",
" 10 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ073 | \n",
" 9 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ078 | \n",
" 6 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ060 | \n",
" 8 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ040 | \n",
" 9 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ065 | \n",
" 9 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ053 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ015 | \n",
" 9 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ021 | \n",
" 9 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ018 | \n",
" 7 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ039 | \n",
" 8 | \n",
" 9 | \n",
"
\n",
" \n",
" М141БПЛТЛ036 | \n",
" 10 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ049 | \n",
" 7 | \n",
" 6 | \n",
"
\n",
" \n",
" 06114043 | \n",
" 8 | \n",
" 10 | \n",
"
\n",
" \n",
" М141БПЛТЛ048 | \n",
" 6 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ034 | \n",
" 9 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ045 | \n",
" 8 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ033 | \n",
" 9 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ083 | \n",
" 5 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ008 | \n",
" 8 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ001 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ038 | \n",
" 9 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ052 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ011 | \n",
" 6 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ004 | \n",
" 7 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ010 | \n",
" 6 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ071 | \n",
" 9 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ035 | \n",
" 6 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ030 | \n",
" 6 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ070 | \n",
" 5 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ051 | \n",
" 9 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ046 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ047 | \n",
" 8 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ063 | \n",
" 5 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ029 | \n",
" 8 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ064 | \n",
" 8 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ076 | \n",
" 7 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ062 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ074 | \n",
" 6 | \n",
" 7 | \n",
"
\n",
" \n",
" 130232038 | \n",
" 7 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ023 | \n",
" 9 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ054 | \n",
" 8 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ012 | \n",
" 6 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ006 | \n",
" 5 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ055 | \n",
" 5 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ007 | \n",
" 7 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ050 | \n",
" 6 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ066 | \n",
" 10 | \n",
" 7 | \n",
"
\n",
" \n",
" М141БПЛТЛ043 | \n",
" 5 | \n",
" 6 | \n",
"
\n",
" \n",
" М141БПЛТЛ084 | \n",
" 7 | \n",
" 8 | \n",
"
\n",
" \n",
" М141БПЛТЛ005 | \n",
" 7 | \n",
" 5 | \n",
"
\n",
" \n",
" М141БПЛТЛ044 | \n",
" 5 | \n",
" 7 | \n",
"
\n",
" \n",
" 13051038 | \n",
" 4 | \n",
" 4 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ024 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ017 | \n",
" 9 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 6.0 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ072 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ060 | \n",
" 7 | \n",
" 8 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 5 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ024 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ017 | \n",
" 9 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 6.0 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ072 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ020 | \n",
" 8 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 10 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 8 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ026 | \n",
" 7 | \n",
" 10 | \n",
" 8 | \n",
" 7 | \n",
" 10 | \n",
" 7 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ024 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ017 | \n",
" 9 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 6.0 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ072 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ026 | \n",
" 7 | \n",
" 10 | \n",
" 8 | \n",
" 7 | \n",
" 10 | \n",
" 7 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ073 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 7 | \n",
" 6.0 | \n",
" 10 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ060 | \n",
" 7 | \n",
" 8 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 5 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ021 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 7.0 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 6.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ018 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 7 | \n",
" 9 | \n",
" 7 | \n",
" 8 | \n",
" 6.0 | \n",
" 6 | \n",
" 7 | \n",
" 8 | \n",
" 7.0 | \n",
" 7 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ039 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 8 | \n",
" 6 | \n",
" 8.0 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 6.0 | \n",
" 7 | \n",
" 8.0 | \n",
" 4 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ036 | \n",
" 8 | \n",
" 10 | \n",
" 7 | \n",
" 8 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 4.0 | \n",
" 8 | \n",
" 8 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ045 | \n",
" 5 | \n",
" 8 | \n",
" 8 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 7 | \n",
" 8 | \n",
" 6.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 5 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ033 | \n",
" 5 | \n",
" 9 | \n",
" 8 | \n",
" 7 | \n",
" 9 | \n",
" 7 | \n",
" 9 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 8 | \n",
" 7.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ008 | \n",
" 10 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 5 | \n",
" 5.0 | \n",
" 10 | \n",
" 4 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ052 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 6.0 | \n",
" 8 | \n",
" 6 | \n",
" 7 | \n",
" 5.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 5 | \n",
" 7 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ071 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 5 | \n",
" NaN | \n",
" 5 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ029 | \n",
" 6 | \n",
" 8 | \n",
" 8 | \n",
" 7 | \n",
" 9 | \n",
" 5 | \n",
" 6 | \n",
" 7.0 | \n",
" 6 | \n",
" 5 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ064 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 4 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 4 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ023 | \n",
" 7 | \n",
" 9 | \n",
" 6 | \n",
" 8 | \n",
" 9 | \n",
" 6 | \n",
" 9 | \n",
" 4.0 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 4 | \n",
" 4.0 | \n",
" 7 | \n",
" 5 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ066 | \n",
" 7 | \n",
" 10 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 5 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 5 | \n",
" 6 | \n",
" 4.0 | \n",
" 6 | \n",
" 4.0 | \n",
" 5 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ008 | \n",
" 10 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 5 | \n",
" 5.0 | \n",
" 10 | \n",
" 4 | \n",
" 1 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ020 | \n",
" 8 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 10 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 8 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ026 | \n",
" 7 | \n",
" 10 | \n",
" 8 | \n",
" 7 | \n",
" 10 | \n",
" 7 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ060 | \n",
" 7 | \n",
" 8 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 5 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ040 | \n",
" 6 | \n",
" 9 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 8 | \n",
" 6.0 | \n",
" 9 | \n",
" 5 | \n",
" 8 | \n",
" 5.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 10 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ015 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 9 | \n",
" 4.0 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 7.0 | \n",
" 10 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ018 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 7 | \n",
" 9 | \n",
" 7 | \n",
" 8 | \n",
" 6.0 | \n",
" 6 | \n",
" 7 | \n",
" 8 | \n",
" 7.0 | \n",
" 7 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ049 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 8 | \n",
" 5 | \n",
" 9 | \n",
" 6.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 6 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ048 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" 4 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ034 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 8 | \n",
" 6.0 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ045 | \n",
" 5 | \n",
" 8 | \n",
" 8 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 7 | \n",
" 8 | \n",
" 6.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 5 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ033 | \n",
" 5 | \n",
" 9 | \n",
" 8 | \n",
" 7 | \n",
" 9 | \n",
" 7 | \n",
" 9 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 8 | \n",
" 7.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ052 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 6.0 | \n",
" 8 | \n",
" 6 | \n",
" 7 | \n",
" 5.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 5 | \n",
" 7 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ011 | \n",
" 7 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 6.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 5 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ004 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 5.0 | \n",
" 7 | \n",
" 5.0 | \n",
" 8 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ010 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 5 | \n",
" 8 | \n",
" 6.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 5 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ071 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 5 | \n",
" NaN | \n",
" 5 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ035 | \n",
" 5 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 8 | \n",
" 5 | \n",
" 5 | \n",
" 4.0 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 5.0 | \n",
" 8 | \n",
" 7.0 | \n",
" 6 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ030 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 4.0 | \n",
" 8 | \n",
" 5 | \n",
" 5 | \n",
" 5.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ051 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 7 | \n",
" 6 | \n",
" 7.0 | \n",
" 6 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 4 | \n",
" 4.0 | \n",
" 5 | \n",
" 5 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ029 | \n",
" 6 | \n",
" 8 | \n",
" 8 | \n",
" 7 | \n",
" 9 | \n",
" 5 | \n",
" 6 | \n",
" 7.0 | \n",
" 6 | \n",
" 5 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ064 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 4 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 4 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ076 | \n",
" 7 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 6.0 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 4.0 | \n",
" 4 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ062 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 6 | \n",
" 5 | \n",
" 6 | \n",
" 4.0 | \n",
" 5 | \n",
" 5.0 | \n",
" 4 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ007 | \n",
" 6 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 5.0 | \n",
" 4 | \n",
" 5.0 | \n",
" 4 | \n",
" 7 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ050 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 4 | \n",
" 5 | \n",
" 4.0 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 4.0 | \n",
" 5 | \n",
" 4.0 | \n",
" 6 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ066 | \n",
" 7 | \n",
" 10 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 5 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 5 | \n",
" 6 | \n",
" 4.0 | \n",
" 6 | \n",
" 4.0 | \n",
" 5 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ026 | \n",
" 7 | \n",
" 10 | \n",
" 8 | \n",
" 7 | \n",
" 10 | \n",
" 7 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ001 | \n",
" 6 | \n",
" 7 | \n",
" 7 | \n",
" 4 | \n",
" 10 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 6.0 | \n",
" 4 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ012 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 4 | \n",
" 10 | \n",
" 6 | \n",
" 5 | \n",
" 4.0 | \n",
" 7 | \n",
" 5 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
" 4.0 | \n",
" 4 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ024 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ017 | \n",
" 9 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 6.0 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 10 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ072 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ020 | \n",
" 8 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 10 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 8 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ026 | \n",
" 7 | \n",
" 10 | \n",
" 8 | \n",
" 7 | \n",
" 10 | \n",
" 7 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ073 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 7 | \n",
" 6.0 | \n",
" 10 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ078 | \n",
" 6 | \n",
" 6 | \n",
" 9 | \n",
" 5 | \n",
" 6 | \n",
" 10 | \n",
" 7 | \n",
" 6.0 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 6.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ060 | \n",
" 7 | \n",
" 8 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 5 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ040 | \n",
" 6 | \n",
" 9 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 8 | \n",
" 6.0 | \n",
" 9 | \n",
" 5 | \n",
" 8 | \n",
" 5.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 10 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ065 | \n",
" 9 | \n",
" 9 | \n",
" 8 | \n",
" 4 | \n",
" 8 | \n",
" 8 | \n",
" 7 | \n",
" 9.0 | \n",
" 8 | \n",
" 5 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ053 | \n",
" 6 | \n",
" 7 | \n",
" 7 | \n",
" 5 | \n",
" 9 | \n",
" 8 | \n",
" 7 | \n",
" 8.0 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 7.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 9 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ015 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 9 | \n",
" 4.0 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 7.0 | \n",
" 10 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ021 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 7.0 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 6.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ018 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 7 | \n",
" 9 | \n",
" 7 | \n",
" 8 | \n",
" 6.0 | \n",
" 6 | \n",
" 7 | \n",
" 8 | \n",
" 7.0 | \n",
" 7 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ039 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 8 | \n",
" 6 | \n",
" 8.0 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 6.0 | \n",
" 7 | \n",
" 8.0 | \n",
" 4 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ036 | \n",
" 8 | \n",
" 10 | \n",
" 7 | \n",
" 8 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 4.0 | \n",
" 8 | \n",
" 8 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ049 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 8 | \n",
" 5 | \n",
" 9 | \n",
" 6.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 6 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" 06114043 | \n",
" 8 | \n",
" 8 | \n",
" 10 | \n",
" 5 | \n",
" 8 | \n",
" 8 | \n",
" 8 | \n",
" 10.0 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" NaN | \n",
" 7 | \n",
" 8.0 | \n",
" 7 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ048 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" 4 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ034 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 8 | \n",
" 6.0 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ045 | \n",
" 5 | \n",
" 8 | \n",
" 8 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 7 | \n",
" 8 | \n",
" 6.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 5 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ033 | \n",
" 5 | \n",
" 9 | \n",
" 8 | \n",
" 7 | \n",
" 9 | \n",
" 7 | \n",
" 9 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 8 | \n",
" 7.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ083 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 5 | \n",
" 8 | \n",
" 7 | \n",
" 6 | \n",
" 5.0 | \n",
" 7 | \n",
" 5 | \n",
" 7 | \n",
" 5.0 | \n",
" 7 | \n",
" 5.0 | \n",
" 4 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ008 | \n",
" 10 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 5 | \n",
" 5.0 | \n",
" 10 | \n",
" 4 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ001 | \n",
" 6 | \n",
" 7 | \n",
" 7 | \n",
" 4 | \n",
" 10 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 6.0 | \n",
" 4 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ038 | \n",
" 7 | \n",
" 9 | \n",
" 6 | \n",
" 4 | \n",
" 9 | \n",
" 6 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 4 | \n",
" 8 | \n",
" 4.0 | \n",
" 5 | \n",
" 4.0 | \n",
" 9 | \n",
" 7 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ052 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 6.0 | \n",
" 8 | \n",
" 6 | \n",
" 7 | \n",
" 5.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 5 | \n",
" 7 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ011 | \n",
" 7 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 6.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 5 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ004 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 5.0 | \n",
" 7 | \n",
" 5.0 | \n",
" 8 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ010 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 5 | \n",
" 8 | \n",
" 6.0 | \n",
" 8 | \n",
" 6.0 | \n",
" 5 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ071 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 5 | \n",
" NaN | \n",
" 5 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ035 | \n",
" 5 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 8 | \n",
" 5 | \n",
" 5 | \n",
" 4.0 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 5.0 | \n",
" 8 | \n",
" 7.0 | \n",
" 6 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ030 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 4.0 | \n",
" 8 | \n",
" 5 | \n",
" 5 | \n",
" 5.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ070 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 4 | \n",
" 8 | \n",
" 6 | \n",
" 5 | \n",
" 5.0 | \n",
" 6 | \n",
" 4 | \n",
" 5 | \n",
" 6.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 6 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ051 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 7 | \n",
" 6 | \n",
" 7.0 | \n",
" 6 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 4 | \n",
" 4.0 | \n",
" 5 | \n",
" 5 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ046 | \n",
" 5 | \n",
" 7 | \n",
" 7 | \n",
" 4 | \n",
" 7 | \n",
" 5 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 5 | \n",
" 7 | \n",
" 5.0 | \n",
" 8 | \n",
" 4.0 | \n",
" 5 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ047 | \n",
" 5 | \n",
" 8 | \n",
" 6 | \n",
" 4 | \n",
" 7 | \n",
" 5 | \n",
" 9 | \n",
" 5.0 | \n",
" 6 | \n",
" 4 | \n",
" 6 | \n",
" 4.0 | \n",
" 7 | \n",
" 4.0 | \n",
" 8 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ063 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 4 | \n",
" 8 | \n",
" 4 | \n",
" 4 | \n",
" 4.0 | \n",
" 5 | \n",
" 4 | \n",
" 5 | \n",
" 4.0 | \n",
" 7 | \n",
" 5.0 | \n",
" 8 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ029 | \n",
" 6 | \n",
" 8 | \n",
" 8 | \n",
" 7 | \n",
" 9 | \n",
" 5 | \n",
" 6 | \n",
" 7.0 | \n",
" 6 | \n",
" 5 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ064 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 4 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 4 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ076 | \n",
" 7 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 6.0 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 4.0 | \n",
" 4 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ062 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 6 | \n",
" 5.0 | \n",
" 6 | \n",
" 5 | \n",
" 6 | \n",
" 4.0 | \n",
" 5 | \n",
" 5.0 | \n",
" 4 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ074 | \n",
" 5 | \n",
" 6 | \n",
" 7 | \n",
" 4 | \n",
" 7 | \n",
" 6 | \n",
" 5 | \n",
" 6.0 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 6.0 | \n",
" 6 | \n",
" 6.0 | \n",
" 8 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" 130232038 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 5 | \n",
" 8 | \n",
" 4 | \n",
" 8 | \n",
" 4.0 | \n",
" 8 | \n",
" 4 | \n",
" 5 | \n",
" 5.0 | \n",
" 6 | \n",
" 4.0 | \n",
" 5 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ023 | \n",
" 7 | \n",
" 9 | \n",
" 6 | \n",
" 8 | \n",
" 9 | \n",
" 6 | \n",
" 9 | \n",
" 4.0 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 4 | \n",
" 4.0 | \n",
" 7 | \n",
" 5 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ054 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 4 | \n",
" 8 | \n",
" 6 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" 4 | \n",
" 8 | \n",
" 4.0 | \n",
" 4 | \n",
" 4.0 | \n",
" 4 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ012 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 4 | \n",
" 10 | \n",
" 6 | \n",
" 5 | \n",
" 4.0 | \n",
" 7 | \n",
" 5 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
" 4.0 | \n",
" 4 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ006 | \n",
" 6 | \n",
" 5 | \n",
" 6 | \n",
" 5 | \n",
" 8 | \n",
" 5 | \n",
" 5 | \n",
" 5.0 | \n",
" 6 | \n",
" 4 | \n",
" 7 | \n",
" 5.0 | \n",
" 7 | \n",
" 5.0 | \n",
" 6 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ055 | \n",
" 6 | \n",
" 5 | \n",
" 6 | \n",
" 4 | \n",
" 7 | \n",
" 7 | \n",
" 4 | \n",
" 8.0 | \n",
" 5 | \n",
" 4 | \n",
" 6 | \n",
" 4.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 4 | \n",
" 5 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ007 | \n",
" 6 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 5.0 | \n",
" 4 | \n",
" 5.0 | \n",
" 4 | \n",
" 7 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ050 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 4 | \n",
" 5 | \n",
" 4.0 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 4.0 | \n",
" 5 | \n",
" 4.0 | \n",
" 6 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ066 | \n",
" 7 | \n",
" 10 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 5 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 5 | \n",
" 6 | \n",
" 4.0 | \n",
" 6 | \n",
" 4.0 | \n",
" 5 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ043 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 5 | \n",
" 8 | \n",
" 5 | \n",
" 6 | \n",
" 5.0 | \n",
" 6 | \n",
" 4 | \n",
" 5 | \n",
" 4.0 | \n",
" 5 | \n",
" NaN | \n",
" 4 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ084 | \n",
" 6 | \n",
" 7 | \n",
" 8 | \n",
" 4 | \n",
" 8 | \n",
" 5 | \n",
" 5 | \n",
" NaN | \n",
" 8 | \n",
" 4 | \n",
" 4 | \n",
" 4.0 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" 7 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ005 | \n",
" 5 | \n",
" 7 | \n",
" 5 | \n",
" 5 | \n",
" 7 | \n",
" 4 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
" 4 | \n",
" 5 | \n",
" 5.0 | \n",
" 4 | \n",
" 4.0 | \n",
" 4 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ044 | \n",
" 4 | \n",
" 5 | \n",
" 7 | \n",
" 4 | \n",
" 6 | \n",
" 4 | \n",
" 4 | \n",
" 5.0 | \n",
" 4 | \n",
" 4 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" NaN | \n",
" 5 | \n",
" 5 | \n",
" 1 | \n",
"
\n",
" \n",
" 13051038 | \n",
" 5 | \n",
" 4 | \n",
" 4 | \n",
" 4 | \n",
" 9 | \n",
" 5 | \n",
" 5 | \n",
" 5.0 | \n",
" 5 | \n",
" 4 | \n",
" 4 | \n",
" NaN | \n",
" 7 | \n",
" 4.0 | \n",
" 4 | \n",
" 4 | \n",
" 1 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ015 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 9 | \n",
" 4.0 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 7.0 | \n",
" 10 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ036 | \n",
" 8 | \n",
" 10 | \n",
" 7 | \n",
" 8 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 4.0 | \n",
" 8 | \n",
" 8 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 6.0 | \n",
" 7 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ049 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 8 | \n",
" 5 | \n",
" 9 | \n",
" 6.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 6 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ048 | \n",
" 8 | \n",
" 6 | \n",
" 8 | \n",
" 6 | \n",
" 9 | \n",
" 6 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" 4 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 7.0 | \n",
" 7 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ071 | \n",
" 6 | \n",
" 9 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 5 | \n",
" NaN | \n",
" 5 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ035 | \n",
" 5 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 8 | \n",
" 5 | \n",
" 5 | \n",
" 4.0 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 5.0 | \n",
" 8 | \n",
" 7.0 | \n",
" 6 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ030 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 4.0 | \n",
" 8 | \n",
" 5 | \n",
" 5 | \n",
" 5.0 | \n",
" 8 | \n",
" 5.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ063 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 4 | \n",
" 8 | \n",
" 4 | \n",
" 4 | \n",
" 4.0 | \n",
" 5 | \n",
" 4 | \n",
" 5 | \n",
" 4.0 | \n",
" 7 | \n",
" 5.0 | \n",
" 8 | \n",
" 8 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ064 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 4 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 4 | \n",
" 7 | \n",
" 0 | \n",
"
\n",
" \n",
" 130232038 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 5 | \n",
" 8 | \n",
" 4 | \n",
" 8 | \n",
" 4.0 | \n",
" 8 | \n",
" 4 | \n",
" 5 | \n",
" 5.0 | \n",
" 6 | \n",
" 4.0 | \n",
" 5 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ023 | \n",
" 7 | \n",
" 9 | \n",
" 6 | \n",
" 8 | \n",
" 9 | \n",
" 6 | \n",
" 9 | \n",
" 4.0 | \n",
" 7 | \n",
" 7 | \n",
" 7 | \n",
" 6.0 | \n",
" 4 | \n",
" 4.0 | \n",
" 7 | \n",
" 5 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ054 | \n",
" 7 | \n",
" 8 | \n",
" 6 | \n",
" 4 | \n",
" 8 | \n",
" 6 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" 4 | \n",
" 8 | \n",
" 4.0 | \n",
" 4 | \n",
" 4.0 | \n",
" 4 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ012 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 4 | \n",
" 10 | \n",
" 6 | \n",
" 5 | \n",
" 4.0 | \n",
" 7 | \n",
" 5 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
" 4.0 | \n",
" 4 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ007 | \n",
" 6 | \n",
" 7 | \n",
" 7 | \n",
" 6 | \n",
" 7 | \n",
" 6 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 5.0 | \n",
" 4 | \n",
" 5.0 | \n",
" 4 | \n",
" 7 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ050 | \n",
" 8 | \n",
" 6 | \n",
" 6 | \n",
" 6 | \n",
" 8 | \n",
" 4 | \n",
" 5 | \n",
" 4.0 | \n",
" 5 | \n",
" 5 | \n",
" 6 | \n",
" 4.0 | \n",
" 5 | \n",
" 4.0 | \n",
" 6 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ066 | \n",
" 7 | \n",
" 10 | \n",
" 7 | \n",
" 7 | \n",
" 9 | \n",
" 5 | \n",
" 8 | \n",
" 4.0 | \n",
" 6 | \n",
" 5 | \n",
" 6 | \n",
" 4.0 | \n",
" 6 | \n",
" 4.0 | \n",
" 5 | \n",
" 6 | \n",
" 0 | \n",
"
\n",
" \n",
" М141БПЛТЛ005 | \n",
" 5 | \n",
" 7 | \n",
" 5 | \n",
" 5 | \n",
" 7 | \n",
" 4 | \n",
" 7 | \n",
" 4.0 | \n",
" 5 | \n",
" 4 | \n",
" 5 | \n",
" 5.0 | \n",
" 4 | \n",
" 4.0 | \n",
" 4 | \n",
" 8 | \n",
" 1 | \n",
"
\n",
" \n",
" М141БПЛТЛ044 | \n",
" 4 | \n",
" 5 | \n",
" 7 | \n",
" 4 | \n",
" 6 | \n",
" 4 | \n",
" 4 | \n",
" 5.0 | \n",
" 4 | \n",
" 4 | \n",
" 4 | \n",
" 4.0 | \n",
" 6 | \n",
" NaN | \n",
" 5 | \n",
" 5 | \n",
" 1 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
" Avg_Soc | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ024 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 10 | \n",
" 1 | \n",
" 8.5 | \n",
"
\n",
" \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
" 10.0 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
" 9.0 | \n",
"
\n",
" \n",
" М141БПЛТЛ017 | \n",
" 9 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 6.0 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
" 8.5 | \n",
"
\n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 10 | \n",
" 1 | \n",
" 9.5 | \n",
"
\n",
" \n",
"
\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",
" Cps | \n",
" Mstat | \n",
" Soc | \n",
" Econ | \n",
" Eng | \n",
" Polth | \n",
" Mstat2 | \n",
" Phist | \n",
" Law | \n",
" Phil | \n",
" Polsoc | \n",
" Ptheo | \n",
" Preg | \n",
" Compp | \n",
" Game | \n",
" Wpol | \n",
" Male | \n",
" Avg_Soc | \n",
"
\n",
" \n",
" id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" М141БПЛТЛ024 | \n",
" 7 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 8 | \n",
" 10 | \n",
" 8 | \n",
" 7 | \n",
" 9 | \n",
" 9 | \n",
" 7.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 6 | \n",
" 10 | \n",
" 1 | \n",
" 8.5 | \n",
"
\n",
" \n",
" М141БПЛТЛ031 | \n",
" 8 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 10 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 9 | \n",
" 10 | \n",
" 1 | \n",
" 10.0 | \n",
"
\n",
" \n",
" М141БПЛТЛ075 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 9 | \n",
" 10 | \n",
" 9 | \n",
" 9.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 7 | \n",
" 9 | \n",
" 1 | \n",
" 9.0 | \n",
"
\n",
" \n",
" М141БПЛТЛ017 | \n",
" 9 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 9 | \n",
" 9 | \n",
" 10 | \n",
" 6 | \n",
" 9 | \n",
" 9 | \n",
" 9 | \n",
" 8.0 | \n",
" 8 | \n",
" 8.0 | \n",
" 8 | \n",
" 9 | \n",
" 0 | \n",
" 8.5 | \n",
"
\n",
" \n",
" М141БПЛТЛ069 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 10 | \n",
" 9 | \n",
" 8 | \n",
" 8 | \n",
" 10 | \n",
" 9 | \n",
" 7.0 | \n",
" 6 | \n",
" 5.0 | \n",
" 8 | \n",
" 10 | \n",
" 1 | \n",
" 9.5 | \n",
"
\n",
" \n",
"
\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+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADvJJREFUeJzt3XusZWV9xvHv44wGhmq9cLAKjgcaghpSBY/GSksrSKOioKa2mNpQax2TWkXbREdjiv80wcR6adpYR7zgDSN4rVoEsWqaWHQGaLiMBqsjjKAz1raoUAH99Y+9pg4jMnv2OXu9c877/SQne601e/b7rNkzPKx7qgpJUr/u0zqAJKkti0CSOmcRSFLnLAJJ6pxFIEmdswgkqXMWgSR1ziKQpM5ZBJLUufWtA0zj8MMPr8XFxdYxJGlV2bZt2/eramF/71sVRbC4uMjWrVtbx5CkVSXJt6d5n7uGJKlzFoEkdc4ikKTOWQSS1DmLQJI6ZxFIUucsAknqnEUgSZ2zCCSpc6viyuLlWNz86Sbj7jjv9Cbjalyt/n6Bf8e0ctwikKTOWQSS1DmLQJI6ZxFIUucsAknqnEUgSZ2zCCSpcxaBJHXOIpCkzlkEktQ5i0CSOmcRSFLnLAJJ6pxFIEmdm1sRJHlXkl1Jrt1r2YOTXJbkhuH1QfMaX5I0nXluEbwHeNo+yzYDl1fVscDlw7wkqaG5FUFVfQn4wT6LzwQuGKYvAJ49r/ElSdMZ+xjBQ6vqFoDh9YiRx5ck7eOgPVicZFOSrUm27t69u3UcSVqzxi6C7yV5GMDwuuuXvbGqtlTVUlUtLSwsjBZQknozdhF8Ejh7mD4b+MTI40uS9jHP00cvBL4MHJdkZ5IXAecBpyW5AThtmJckNbR+Xh9cVc//Jb906rzGlCQduIP2YLEkaRwWgSR1ziKQpM5ZBJLUOYtAkjpnEUhS5ywCSeqcRSBJnbMIJKlzFoEkdc4ikKTOWQSS1DmLQJI6N7e7j/ZucfOnm42947zTm40tafVxi0CSOmcRSFLnLAJJ6pxFIEmdswgkqXMWgSR1ziKQpM5ZBJLUOYtAkjpnEUhS5ywCSeqcRSBJnbMIJKlzFoEkdc4ikKTONSmCJK9Mcl2Sa5NcmOSQFjkkSQ2KIMmRwMuBpao6HlgHnDV2DknSRKtdQ+uBQ5OsBzYANzfKIUndG/1RlVX1nSRvBG4EbgcurapL931fkk3AJoCNGzeOG1Iz8fGc42r1593jn/Va12LX0IOAM4GjgYcDhyV5wb7vq6otVbVUVUsLCwtjx5SkbrTYNfRU4FtVtbuq7gQ+Cjy5QQ5JEm2K4EbgSUk2JAlwKrC9QQ5JEg2KoKquAC4GrgSuGTJsGTuHJGli9IPFAFV1LnBui7ElSXfnlcWS1DmLQJI6ZxFIUucsAknqnEUgSZ2zCCSpcxaBJHXOIpCkzlkEktQ5i0CSOmcRSFLnLAJJ6txURZDk+HkHkSS1Me0WwT8m+UqSP0/ywLkmkiSNaqoiqKrfAv4IeASwNckHk5w212SSpFFMfYygqm4AXge8Gvgd4O+SfC3Jc+cVTpI0f9MeI/iNJG9m8kjJU4BnVdWjh+k3zzGfJGnOpn1C2d8D7wBeW1W371lYVTcned1ckkmSRjFtETwDuL2qfgqQ5D7AIVV1W1W9b27pJElzN+0xgs8Bh+41v2FYJkla5aYtgkOq6kd7ZobpDfOJJEka07RF8OMkJ+6ZSfJ44PZ7eb8kaZWY9hjBK4CLktw8zD8M+MP5RJIkjWmqIqiqryZ5FHAcEOBrVXXnXJNJkkYx7RYBwBOAxeH3nJCEqnrvXFJJkkYzVREkeR/w68DVwE+HxQVYBJK0yk27RbAEPKaqap5hJEnjm/asoWuBX5tnEElSG9NuERwOXJ/kK8BP9iysqjNmGXS4lfX5wPFMdjH9aVV9eZbPkiQtz7RF8PoVHvetwCVV9ftJ7ocXp0lSM9OePvrFJI8Ejq2qzyXZAKybZcAkDwBOBv5k+Ow7gDtm+SxJ0vJNexvqFwMXA28fFh0JfHzGMY8BdgPvTnJVkvOTHDbjZ0mSlmnag8UvBU4CboX/f0jNETOOuR44EXhbVZ0A/BjYvO+bkmxKsjXJ1t27d884lCRpf6Ytgp8Mu3AASLKeyUHeWewEdlbVFcP8xUyK4W6qaktVLVXV0sLCwoxDSZL2Z9oi+GKS1wKHDs8qvgj4p1kGrKrvAjclOW5YdCpw/SyfJUlavmnPGtoMvAi4BngJ8Bkmp3/O6mXAB4Yzhr4JvHAZnyVJWoZpzxr6GZNHVb5jJQatqquZXK0sSWps2nsNfYt7OCZQVceseCJJ0qgO5F5DexwCPA948MrHkSSNbaqDxVX1n3v9fKeq3gKcMudskqQRTLtraO/TO+/DZAvh/nNJJEka1bS7hv52r+m7gB3AH6x4GknS6KY9a+gp8w4iSWpj2l1Df3lvv15Vb1qZOJKksR3IWUNPAD45zD8L+BJw0zxCSZLGcyAPpjmxqn4IkOT1wEVV9WfzCiZJGse09xrayN2fGXAHsLjiaSRJo5t2i+B9wFeSfIzJFcbPAd47t1SSpNFMe9bQ3yT5Z+C3h0UvrKqr5hdLkjSWaXcNweS5wrdW1VuBnUmOnlMmSdKIpn1U5bnAq4HXDIvuC7x/XqEkSeOZdovgOcAZTB4rSVXdjLeYkKQ1YdoiuKOqiuFW1D5sXpLWjmmL4MNJ3g48MMmLgc+xQg+pkSS1Ne1ZQ28cnlV8K3Ac8NdVddlck0mSRrHfIkiyDvhsVT0V8D/+krTG7HfXUFX9FLgtya+OkEeSNLJpryz+X+CaJJcxnDkEUFUvn0sqSdJopi2CTw8/kqQ15l6LIMnGqrqxqi4YK5AkaVz7O0bw8T0TST4y5yySpAb2VwTZa/qYeQaRJLWxvyKoXzItSVoj9new+LFJbmWyZXDoMM0wX1X1gLmmkyTN3b0WQVWtGyuIJKmNA3kegSRpDWpWBEnWJbkqyadaZZAktd0iOAfY3nB8SRKNiiDJUcDpwPktxpck/dy0t5hYaW8BXsW9POUsySZgE8DGjRtHiiXpYLa4uc2dbnacd3qTcccy+hZBkmcCu6pq2729r6q2VNVSVS0tLCyMlE6S+tNi19BJwBlJdgAfAk5J8v4GOSRJNCiCqnpNVR1VVYvAWcDnq+oFY+eQJE14HYEkda7VwWIAquoLwBdaZpCk3rlFIEmdswgkqXMWgSR1ziKQpM5ZBJLUOYtAkjpnEUhS5ywCSeqcRSBJnbMIJKlzFoEkdc4ikKTOWQSS1Lmmdx+VpNVgrT8i0y0CSeqcRSBJnbMIJKlzFoEkdc4ikKTOWQSS1DmLQJI6ZxFIUucsAknqnEUgSZ2zCCSpcxaBJHXOIpCkzlkEktS50YsgySOS/EuS7UmuS3LO2BkkST/X4nkEdwF/VVVXJrk/sC3JZVV1fYMsktS90bcIquqWqrpymP4hsB04cuwckqSJpscIkiwCJwBXtMwhST1rVgRJfgX4CPCKqrr1Hn59U5KtSbbu3r17/ICS1IkmRZDkvkxK4ANV9dF7ek9VbamqpapaWlhYGDegJHWkxVlDAd4JbK+qN409viTp7lpsEZwE/DFwSpKrh59nNMghSaLB6aNV9a9Axh5XknTPvLJYkjpnEUhS5ywCSeqcRSBJnbMIJKlzFoEkdc4ikKTOWQSS1DmLQJI6ZxFIUucsAknqnEUgSZ2zCCSpcxaBJHXOIpCkzlkEktQ5i0CSOmcRSFLnLAJJ6pxFIEmdswgkqXMWgSR1ziKQpM5ZBJLUOYtAkjpnEUhS5ywCSeqcRSBJnbMIJKlzTYogydOSfD3JN5JsbpFBkjQxehEkWQf8A/B04DHA85M8ZuwckqSJFlsETwS+UVXfrKo7gA8BZzbIIUmiTREcCdy01/zOYZkkqYH1DcbMPSyrX3hTsgnYNMz+KMnXZxzvcOD7M/7eg81U65I3jJBkeVb8O2m4zv79Ojitie8lb1j2ejxymje1KIKdwCP2mj8KuHnfN1XVFmDLcgdLsrWqlpb7OQeDtbIua2U9wHU5WK2VdRlrPVrsGvoqcGySo5PcDzgL+GSDHJIkGmwRVNVdSf4C+CywDnhXVV03dg5J0kSLXUNU1WeAz4w03LJ3Lx1E1sq6rJX1ANflYLVW1mWU9UjVLxynlSR1xFtMSFLn1nQRJFmX5Kokn2qdZTmS7EhyTZKrk2xtnWc5kjwwycVJvpZke5LfbJ1pFkmOG76PPT+3JnlF61yzSPLKJNcluTbJhUkOaZ1pVknOGdbjutX2fSR5V5JdSa7da9mDk1yW5Ibh9UHzGHtNFwFwDrC9dYgV8pSqetwaOCXurcAlVfUo4LGs0u+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": [
"