Last active
August 30, 2021 04:30
-
-
Save hsteinshiromoto/2cfc1173b01d333139c3a60291359c3e to your computer and use it in GitHub Desktop.
Select the row closest to a selected value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"toc": true | |
}, | |
"source": [ | |
"<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n", | |
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Import\" data-toc-modified-id=\"Import-1\"><span class=\"toc-item-num\">1 </span>Import</a></span><ul class=\"toc-item\"><li><span><a href=\"#Modules\" data-toc-modified-id=\"Modules-1.1\"><span class=\"toc-item-num\">1.1 </span>Modules</a></span></li><li><span><a href=\"#Generating-Data\" data-toc-modified-id=\"Generating-Data-1.2\"><span class=\"toc-item-num\">1.2 </span>Generating Data</a></span></li></ul></li><li><span><a href=\"#Problem-Statement\" data-toc-modified-id=\"Problem-Statement-2\"><span class=\"toc-item-num\">2 </span>Problem Statement</a></span></li><li><span><a href=\"#Solution\" data-toc-modified-id=\"Solution-3\"><span class=\"toc-item-num\">3 </span>Solution</a></span><ul class=\"toc-item\"><li><span><a href=\"#Explanation\" data-toc-modified-id=\"Explanation-3.1\"><span class=\"toc-item-num\">3.1 </span>Explanation</a></span></li></ul></li><li><span><a href=\"#References\" data-toc-modified-id=\"References-4\"><span class=\"toc-item-num\">4 </span>References</a></span></li></ul></div>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Import" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Modules" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-06-04T04:36:23.710701Z", | |
"start_time": "2020-06-04T04:36:23.295959Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Generating Data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-06-04T04:36:23.718185Z", | |
"start_time": "2020-06-04T04:36:23.712717Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"df = pd.DataFrame.from_dict({\"delta_n\": np.random.rand(100, 1).squeeze(), \n", | |
" \"col1\": np.random.randint(-100, 100, size=(100, 1)).squeeze()})" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Problem Statement" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Select the row closest to a selected `value` in the column `delta_n`." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Solution" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Let" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-06-04T04:36:23.726088Z", | |
"start_time": "2020-06-04T04:36:23.720241Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"value = 0.05" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-06-04T04:36:23.749972Z", | |
"start_time": "2020-06-04T04:36:23.728031Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"delta_n 0.048184\n", | |
"col1 79.000000\n", | |
"Name: 92, dtype: float64" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"idx = df['delta_n'].sub(value).abs().idxmin()\n", | |
"df.loc[idx, :]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Explanation" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Subtract `value` from the `delta_n` column" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-06-04T04:36:23.759891Z", | |
"start_time": "2020-06-04T04:36:23.752279Z" | |
}, | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 0.487641\n", | |
"1 0.208109\n", | |
"2 0.221288\n", | |
"3 0.524160\n", | |
"4 0.559814\n", | |
" ... \n", | |
"95 0.096815\n", | |
"96 0.819692\n", | |
"97 0.363321\n", | |
"98 0.630596\n", | |
"99 0.457670\n", | |
"Name: delta_n, Length: 100, dtype: float64" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"interim_result_1 = df['delta_n'].sub(value)\n", | |
"interim_result_1" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Get absolute values of this subtraction" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-06-04T04:36:23.772031Z", | |
"start_time": "2020-06-04T04:36:23.763792Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 0.487641\n", | |
"1 0.208109\n", | |
"2 0.221288\n", | |
"3 0.524160\n", | |
"4 0.559814\n", | |
" ... \n", | |
"95 0.096815\n", | |
"96 0.819692\n", | |
"97 0.363321\n", | |
"98 0.630596\n", | |
"99 0.457670\n", | |
"Name: delta_n, Length: 100, dtype: float64" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"interim_result_2 = interim_result_1.abs()\n", | |
"interim_result_2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Get the index of the minimum value of the absolute values" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-06-04T04:36:23.783889Z", | |
"start_time": "2020-06-04T04:36:23.776136Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"92" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"idx = interim_result_2.idxmin()\n", | |
"idx" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Get the closest row" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-06-04T04:36:23.795129Z", | |
"start_time": "2020-06-04T04:36:23.786703Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"delta_n 0.048184\n", | |
"col1 79.000000\n", | |
"Name: 92, dtype: float64" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.loc[idx, :]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# References" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"https://stackoverflow.com/questions/52587436/find-row-closest-value-to-input" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.7.3" | |
}, | |
"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": true, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": true | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment