{ "cells": [ { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "### Библиотеки / данные " ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "импортируем numpy и pandas" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "настройки pandas" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.options.display.max_rows = 10" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "- считываем данные\n", "- используем столбец Symbol в качестве индекса \n", "- считываем только столбцы ['Symbol', 'Sector', 'Price', 'Book Value']" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "| Column Name | Description\n", "| ------------- |:-------------:|\n", "|Symbol|Сокращенное название организации|\n", "|Name|Полное название организации|\n", "|Sector|Сектор экономики|\n", "|Price|Стоимость акции|\n", "|Dividend Yield|Дивидендная доходность|\n", "|Price/Earnings|Цена / прибыль|\n", "|Earnings/Share|Прибыль на акцию|\n", "|Book Value|Балансовая стоимость компании|\n", "|52 week low|52-недельный минимум|\n", "|52 week high|52-недельный максимум|\n", "|Market Cap|Рыночная капитализация|\n", "|EBITDA|**E**arnings **b**efore **i**nterest, **t**axes, **d**epreciation and **a**mortization|\n", "|Price/Sales|Цена / объём продаж|\n", "|Price/Book|Цена / балансовая стоимость|\n", "|SEC Filings|Ссылка *sec.gov*|" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "sp500 = pd.read_csv(\"../data/sp500.csv\",\n", " index_col='Symbol', \n", " usecols=['Symbol', 'Sector', 'Price', 'Book Value'])" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "### Мотивация" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "создаем DataFame для примера" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "np.random.seed(123)\n", "df = pd.DataFrame({'value':np.random.random(10000), 'key':range(100, 10100)})\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "отбираем строку, в котором значение столбца key равно 10099" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df[df.key==10099]" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "измеряем время выполнения операции отбора" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "%timeit df[df.key==10099]" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "превращаем столбец key в index" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df_with_index = df.set_index(['key'])\n", "df_with_index.head()" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "теперь можно найти это значение с помощью индекса" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df_with_index.loc[10099]" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "и теперь операция выполняется намного быстрее" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "%timeit df_with_index.loc[10099]" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "