Skip to content

Instantly share code, notes, and snippets.

@jrjames83
Created January 2, 2019 22:17
Show Gist options
  • Save jrjames83/97edf39b6aa7929dc5eda8379dcb2b76 to your computer and use it in GitHub Desktop.
Save jrjames83/97edf39b6aa7929dc5eda8379dcb2b76 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import geopy.distance\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def combine_lat_long(df_obj):\n",
" return (df_obj.lat, df_obj.long)\n",
"\n",
"def get_distance(df_obj):\n",
" try:\n",
" return geopy.distance.distance(df_obj.latlong, df_obj.next_lat_long).m\n",
" except ValueError:\n",
" return 0"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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>lat</th>\n",
" <th>long</th>\n",
" <th>latlong</th>\n",
" <th>next_lat_long</th>\n",
" <th>distance_between</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>10.354395</td>\n",
" <td>53.594442</td>\n",
" <td>(10.354395, 53.5944416667)</td>\n",
" <td>(10.354395, 53.5944416667)</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>10.354395</td>\n",
" <td>53.594442</td>\n",
" <td>(10.354395, 53.5944416667)</td>\n",
" <td>(10.3543983333, 53.5944416667)</td>\n",
" <td>0.368701</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>10.354398</td>\n",
" <td>53.594442</td>\n",
" <td>(10.3543983333, 53.5944416667)</td>\n",
" <td>(10.3544016667, 53.59444)</td>\n",
" <td>0.411409</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>10.354402</td>\n",
" <td>53.594440</td>\n",
" <td>(10.3544016667, 53.59444)</td>\n",
" <td>(10.3543983333, 53.5944383333)</td>\n",
" <td>0.411409</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>10.354398</td>\n",
" <td>53.594438</td>\n",
" <td>(10.3543983333, 53.5944383333)</td>\n",
" <td>(10.3543983333, 53.5944383333)</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>10.354398</td>\n",
" <td>53.594438</td>\n",
" <td>(10.3543983333, 53.5944383333)</td>\n",
" <td>(10.3543983333, 53.5944383333)</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>10.354398</td>\n",
" <td>53.594438</td>\n",
" <td>(10.3543983333, 53.5944383333)</td>\n",
" <td>(10.3543966667, 53.59444)</td>\n",
" <td>0.259427</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>10.354397</td>\n",
" <td>53.594440</td>\n",
" <td>(10.3543966667, 53.59444)</td>\n",
" <td>(10.3543933333, 53.59444)</td>\n",
" <td>0.368701</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>10.354393</td>\n",
" <td>53.594440</td>\n",
" <td>(10.3543933333, 53.59444)</td>\n",
" <td>(10.3543933333, 53.59444)</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>10.354393</td>\n",
" <td>53.594440</td>\n",
" <td>(10.3543933333, 53.59444)</td>\n",
" <td>(10.354395, 53.59444)</td>\n",
" <td>0.184350</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" lat long latlong \\\n",
"0 10.354395 53.594442 (10.354395, 53.5944416667) \n",
"1 10.354395 53.594442 (10.354395, 53.5944416667) \n",
"2 10.354398 53.594442 (10.3543983333, 53.5944416667) \n",
"3 10.354402 53.594440 (10.3544016667, 53.59444) \n",
"4 10.354398 53.594438 (10.3543983333, 53.5944383333) \n",
"5 10.354398 53.594438 (10.3543983333, 53.5944383333) \n",
"6 10.354398 53.594438 (10.3543983333, 53.5944383333) \n",
"7 10.354397 53.594440 (10.3543966667, 53.59444) \n",
"8 10.354393 53.594440 (10.3543933333, 53.59444) \n",
"9 10.354393 53.594440 (10.3543933333, 53.59444) \n",
"\n",
" next_lat_long distance_between \n",
"0 (10.354395, 53.5944416667) 0.000000 \n",
"1 (10.3543983333, 53.5944416667) 0.368701 \n",
"2 (10.3544016667, 53.59444) 0.411409 \n",
"3 (10.3543983333, 53.5944383333) 0.411409 \n",
"4 (10.3543983333, 53.5944383333) 0.000000 \n",
"5 (10.3543983333, 53.5944383333) 0.000000 \n",
"6 (10.3543966667, 53.59444) 0.259427 \n",
"7 (10.3543933333, 53.59444) 0.368701 \n",
"8 (10.3543933333, 53.59444) 0.000000 \n",
"9 (10.354395, 53.59444) 0.184350 "
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_csv('/Users/jeff/Downloads/Coordinates.csv')\n",
"df.columns = ['lat', 'long']\n",
"df['latlong'] = df.apply(combine_lat_long, axis=1)\n",
"df['next_lat_long'] = df.latlong.shift(-1)\n",
"df['distance_between'] = df.apply(get_distance, axis=1)\n",
"\n",
"df.head(10)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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>lat</th>\n",
" <th>long</th>\n",
" <th>latlong</th>\n",
" <th>next_lat_long</th>\n",
" <th>distance_between</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>10.354398</td>\n",
" <td>53.594442</td>\n",
" <td>(10.3543983333, 53.5944416667)</td>\n",
" <td>(10.3544016667, 53.59444)</td>\n",
" <td>0.411409</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>10.354402</td>\n",
" <td>53.594440</td>\n",
" <td>(10.3544016667, 53.59444)</td>\n",
" <td>(10.3543983333, 53.5944383333)</td>\n",
" <td>0.411409</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>10.354395</td>\n",
" <td>53.594440</td>\n",
" <td>(10.354395, 53.59444)</td>\n",
" <td>(10.3543933333, 53.5944366667)</td>\n",
" <td>0.408968</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>10.354393</td>\n",
" <td>53.594437</td>\n",
" <td>(10.3543933333, 53.5944366667)</td>\n",
" <td>(10.3543833333, 53.5944283333)</td>\n",
" <td>1.434015</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>10.354383</td>\n",
" <td>53.594428</td>\n",
" <td>(10.3543833333, 53.5944283333)</td>\n",
" <td>(10.3543766667, 53.5944166667)</td>\n",
" <td>1.475235</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>10.354377</td>\n",
" <td>53.594417</td>\n",
" <td>(10.3543766667, 53.5944166667)</td>\n",
" <td>(10.3543666667, 53.594405)</td>\n",
" <td>1.689976</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>10.354367</td>\n",
" <td>53.594405</td>\n",
" <td>(10.3543666667, 53.594405)</td>\n",
" <td>(10.3543566667, 53.594395)</td>\n",
" <td>1.556564</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>10.354357</td>\n",
" <td>53.594395</td>\n",
" <td>(10.3543566667, 53.594395)</td>\n",
" <td>(10.354345, 53.5943866667)</td>\n",
" <td>1.580571</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>10.354345</td>\n",
" <td>53.594387</td>\n",
" <td>(10.354345, 53.5943866667)</td>\n",
" <td>(10.3543366667, 53.594375)</td>\n",
" <td>1.575495</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" lat long latlong \\\n",
"2 10.354398 53.594442 (10.3543983333, 53.5944416667) \n",
"3 10.354402 53.594440 (10.3544016667, 53.59444) \n",
"12 10.354395 53.594440 (10.354395, 53.59444) \n",
"13 10.354393 53.594437 (10.3543933333, 53.5944366667) \n",
"14 10.354383 53.594428 (10.3543833333, 53.5944283333) \n",
"15 10.354377 53.594417 (10.3543766667, 53.5944166667) \n",
"16 10.354367 53.594405 (10.3543666667, 53.594405) \n",
"17 10.354357 53.594395 (10.3543566667, 53.594395) \n",
"18 10.354345 53.594387 (10.354345, 53.5943866667) \n",
"\n",
" next_lat_long distance_between \n",
"2 (10.3544016667, 53.59444) 0.411409 \n",
"3 (10.3543983333, 53.5944383333) 0.411409 \n",
"12 (10.3543933333, 53.5944366667) 0.408968 \n",
"13 (10.3543833333, 53.5944283333) 1.434015 \n",
"14 (10.3543766667, 53.5944166667) 1.475235 \n",
"15 (10.3543666667, 53.594405) 1.689976 \n",
"16 (10.3543566667, 53.594395) 1.556564 \n",
"17 (10.354345, 53.5943866667) 1.580571 \n",
"18 (10.3543366667, 53.594375) 1.575495 "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Then you can filter\n",
"df.query('distance_between > .4')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"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.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment