Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chetanambi/9abab846ea9c86ee022bcd2136934a3d to your computer and use it in GitHub Desktop.
Save chetanambi/9abab846ea9c86ee022bcd2136934a3d to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "markdown",
"id": "eb295de5",
"metadata": {},
"source": [
"# Speed Comparison"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f3c38deb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(237519, 7)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"df = pd.read_csv('Suicides in India 2001-2012.csv')\n",
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "3e8af1cf",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>State</th>\n",
" <th>Year</th>\n",
" <th>Type_code</th>\n",
" <th>Type</th>\n",
" <th>Gender</th>\n",
" <th>Age_group</th>\n",
" <th>Total</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>A &amp; N Islands</td>\n",
" <td>2001</td>\n",
" <td>Causes</td>\n",
" <td>Illness (Aids/STD)</td>\n",
" <td>Female</td>\n",
" <td>0-14</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>A &amp; N Islands</td>\n",
" <td>2001</td>\n",
" <td>Causes</td>\n",
" <td>Bankruptcy or Sudden change in Economic</td>\n",
" <td>Female</td>\n",
" <td>0-14</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" State Year Type_code Type \\\n",
"0 A & N Islands 2001 Causes Illness (Aids/STD) \n",
"1 A & N Islands 2001 Causes Bankruptcy or Sudden change in Economic \n",
"\n",
" Gender Age_group Total \n",
"0 Female 0-14 0 \n",
"1 Female 0-14 0 "
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head(2)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "7f45cad2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"27.8 s ± 3.84 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for idx, row in df.iterrows():\n",
" row['State'], row['Year'], row['Type']"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "41477099",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"496 ms ± 140 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for row in df.itertuples():\n",
" row.State, row.Year, row.Type"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "afddd65e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8.69 s ± 381 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for idx in range(len(df)):\n",
" df.loc[idx, \"State\"], df.loc[idx, \"Year\"], df.loc[idx, 'Type']"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "6a6cfdf4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"37.3 s ± 11.8 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for idx in range(len(df)):\n",
" df.iloc[idx, 0], df.iloc[idx, 1], df.iloc[idx, 3]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "0e3269bf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8.2 s ± 1.15 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for idx in df.index:\n",
" df['State'][idx], df['Year'][idx], df['Type'][idx]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "6a6f9f8b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.99 s ± 272 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for item in df.to_dict(orient='records'):\n",
" item['State'], item['Year'], item['Type']"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "fed012c6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"211 ms ± 25 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"for item in df.to_numpy():\n",
" item[0], item[1], item[3]"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "301be1c3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"50 s ± 8.64 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for key, value in df.T.iteritems():\n",
" value.State, value.Year, value.Type"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "ec498337",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"52.8 s ± 13 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for key, value in df.T.items():\n",
" value.State, value.Year, value.Type"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment