{ "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": { "heading_collapsed": true }, "source": [ "### Слияние датафреймов" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "**Типы соединений**\n", "" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "**Аргументы функции merge**\n", "\n", "\n", "| Аргумент | Описание\n", "| ------------- |:-------------:|\n", "|left|Объект DataFrame в левой части операции слияния|\n", "|right|Объект DataFrame в правой части операции слияния|\n", "|how|Допустимые значения: 'inner', 'outer', 'left', 'right '|\n", "|on|Имена столбцов, по которым производится соединение. Должны присутствовать в обоих объектах DataFrame. Если не заданы и не указаны никакие другие ключи соединения, то используются имена столбцов, общих для обоих объектов|\n", "|left_on|Столбцы левого DataFrame, используемые как ключи соединения|\n", "|right_on|Столбцы правого DataFrame, используемые как ключи соединения|\n", "|left index|Использовать индекс строк левого DataFrame в качестве его ключа соединения (или нескольких ключей в случае мультииндекса)|\n", "|right_index|То же, что left_index, но для правого DataFrame|\n", "|sort|Сортировать слитые данные лексикографически по ключам соединения по умолчанию True. Иногда при работе с большими наборами данных лучше отключить\n", "|suffixes|Кортеж строк, которые дописываются в конец совпадающих имен столбцов;\n", "|сору|Если равен False, то в некоторых особых случаях разрешается не копировать данные в результирующую структуру.|" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "для примеров:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df1 = pd.DataFrame({'key': ['a','a', 'a', 'b', 'b', 'c', 'd'],\n", " 'data1': range(7)},\n", " index = range(3,10))\n", "df2 = pd.DataFrame({'key': ['a', 'b', 'b', 'f'],\n", " 'data2': range(4)})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df2" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "по умолчанию:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.merge(df1, df2)" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "по каким столбцам соединять?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.merge(df1, df2, on='key')" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "разные имена столбцов:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.merge(df1, df2, left_on='data1', right_on='data2')" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "задание суффиксовов:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.merge(df1, df2, left_on='data1', right_on='data2',suffixes=('_left', '_right'))" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "соединение по нескольким столбцам:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.merge(df1, df2,\n", " left_on=['data1', 'key'],\n", " right_on=['data2', 'key'])" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "указываем тип соединения:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.merge(df1, df2, how='outer')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.merge(df1, df2, left_index=True, right_on='data2')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.merge(df1, df2, left_on='data1', right_index=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.merge(df1, df2, left_index=True, right_index=True)" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "### Конкатенация датафреймов" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "для примеров:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df1 = pd.DataFrame(index = range(3), data = np.arange(9).reshape(3, 3), \n", " columns=['a', 'b', 'c'])\n", "df2 = pd.DataFrame(index = range(2,5), data = np.arange(9, 18).reshape(3, 3), \n", " columns=['a', 'c', 'd'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df2" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "По умолчанию:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.concat([df1, df2], sort=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.concat([df1, df2], axis=0, sort=True, join='inner')" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "По оси столбцов:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "pd.concat([df1, df2], axis=1, sort=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "df2" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "### \"Long” и “Wide”" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "считываем данные акселерометра (\"Long\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "long_format = pd.read_csv(\"../data/accel.csv\")\n", "long_format" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "\"Wide\" формат, используем unstack:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "tmp = long_format.set_index(['interval','axis'])\n", "tmp" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "tmp = tmp.unstack()\n", "tmp" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "\"Wide\" формат, используем pivot:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "wide_format = long_format.pivot(index='interval', \n", " columns='axis', \n", " values='reading')\n", "\n", "wide_format" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "Из \"Wide\" в \"Long\" формат:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "long_format_stack = wide_format.stack()\n", "long_format_stack" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "создаем две копии данных акселерометра, по одной для каждого пользователя" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "user1 = long_format.copy()\n", "user2 = long_format.copy()" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "добавляем столбец who в каждую копию" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "user1['who'] = 'Mike'\n", "user2['who'] = 'David'" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "отмасштабируем данные user2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "user2['reading'] *= 100" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "user1.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "user2.head()" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "организуем данные так, чтобы получить иерархический индекс строк" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "scrolled": false }, "outputs": [], "source": [ "multi_user_long_format = pd.concat([user1, user2]).set_index(['who', 'interval', 'axis'])\n", "multi_user_long_format" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "пример для повторения извлекаем все показания по всем осям и по всем пользователям при interval == 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "multi_user_long_format.xs(1, level='interval')" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "выполняем расстыковку, в результате 2-й уровень индекса строк (уровень axis) стал уровнем индекса столбцов" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "scrolled": true }, "outputs": [], "source": [ "multi_user_wide_format = multi_user_long_format.unstack()\n", "multi_user_wide_format" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "параметр level:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "multi_user_long_format.unstack(level = 1)" ] } ], "metadata": { "anaconda-cloud": {}, "hide_input": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 1 }