{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Сравниваем скорость методов" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from random import randrange\n", "super_a = [[randrange(int(1e5)), randrange(int(1e5))] for i in range(int(1e7))]\n", "df = pd.DataFrame(super_a, columns=['some','data'])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
somedata
06888865957
16977122811
23151570784
37934945280
\n", "
" ], "text/plain": [ " some data\n", "0 68888 65957\n", "1 69771 22811\n", "2 31515 70784\n", "3 79349 45280" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# получилось\n", "df.head(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## бигдата" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000000, 2)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "df.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Тут сравниваемые способы\n" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "\n", "#будем брать четные числа и умножать на 100\n", "def m1(df):\n", " f = df['some'] % 2 == 0\n", " df.loc[f,'some'] = df.loc[f,'some'] * 100 \n", " return df\n", "\n", "def m2(df):\n", " df['some'].apply(lambda x : x * 100 if x % 2 == 0 else x)\n", " return df\n", "\n", "def m3(df):\n", " df['some'].where(df['some'] % 2 == 0, df['some'] * 100, inplace=True)\n", " return df\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "826 ms ± 12.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit m1(df)" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.11 s ± 55.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit m2(df)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "298 ms ± 481 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit m3(df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.7.3 64-bit", "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.3" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1" } } }, "nbformat": 4, "nbformat_minor": 2 }