Getting index of minimum distance in Panda's dataframe












1















The head of my data as follows:



   lat1 long1 state           county   lat2  long2  dist Depot
0 . . AK Aleutians West 11.0 23.0 121 0.0
1 . . AK Wade Hampton 33.0 11.0 12 0.0
2 . . AK North Slope 55.0 11.0 43 0.0
3 . . AK Kenai Peninsula 44.0 11.0 43 0.0
4 . . AK Anchorage 11.0 11.0 99 0.0
5 1 2 AK Anchorage NaN NaN 1e10 0.0
6 . . AK Anchorage 55.0 44.0 33 0.0
7 3 4 AK Anchorage NaN NaN 1e10 0.0
8 . . AK Anchorage 3.0 2.0 23 0.0
9 . . AK Anchorage 5.0 11.0 32 0.0
10 . . AK Anchorage 42.0 22.0 33 0.0
11 . . AK Anchorage 11.0 2.0 33 0.0
12 . . AK Anchorage 444.0 1.0 43 0.0
13 . . AK Anchorage 1.0 2.0 53 0.0
14 0 2 AK Anchorage NaN NaN 1e10 0.0
15 . . AK Anchorage 1.0 1.0 32 0.0
16 . . AK Anchorage 111.0 11.0 22 0.0


The distance column indicates the minimum distance between the corresponding lat2/long2 and all the entries in lat1/long1. My code for the same is as follows:



import pandas as pd
from haversine import haversine
import numpy as np

path = 'distance.xlsx'
df = pd.read_excel(path, parse_cols = [1,2,4,5,7,8])
df = df.assign(dist=pd.Series(np.zeros(27055)).values);
df = df.assign(Depot=pd.Series(np.zeros(27055)).values);


temp = 1e10
for i in range(0, len(df)):
for j in range(0, len(df)):
if df.iloc[j]['lat1'] is '.' and df.iloc[j]['long1'] is '.':
continue
else:
p1 = (df.iloc[j]['lat1'],df.iloc[j]['long1'])
p2 = (df.iloc[i]['lat2'],df.iloc[i]['long2'])
df.dist.iloc[i] = min(temp,haversine(p1, p2, miles=True))
temp = df.dist.iloc[i]


temp = 1e10


For the minimum distance for each index, I want to know the corresponding p2 (lat1,long1) and store it in the Depot column. I understand this must be some indexing command but I'm struggling with the exact form.










share|improve this question

























  • Could you elaborate on the sentence "For the minimum distance for each index, I want to know the corresponding p2 (lat1,long1) and store it in the Depot column"? I didn't understand what you asked for :)

    – Gal Avineri
    Nov 19 '18 at 18:39
















1















The head of my data as follows:



   lat1 long1 state           county   lat2  long2  dist Depot
0 . . AK Aleutians West 11.0 23.0 121 0.0
1 . . AK Wade Hampton 33.0 11.0 12 0.0
2 . . AK North Slope 55.0 11.0 43 0.0
3 . . AK Kenai Peninsula 44.0 11.0 43 0.0
4 . . AK Anchorage 11.0 11.0 99 0.0
5 1 2 AK Anchorage NaN NaN 1e10 0.0
6 . . AK Anchorage 55.0 44.0 33 0.0
7 3 4 AK Anchorage NaN NaN 1e10 0.0
8 . . AK Anchorage 3.0 2.0 23 0.0
9 . . AK Anchorage 5.0 11.0 32 0.0
10 . . AK Anchorage 42.0 22.0 33 0.0
11 . . AK Anchorage 11.0 2.0 33 0.0
12 . . AK Anchorage 444.0 1.0 43 0.0
13 . . AK Anchorage 1.0 2.0 53 0.0
14 0 2 AK Anchorage NaN NaN 1e10 0.0
15 . . AK Anchorage 1.0 1.0 32 0.0
16 . . AK Anchorage 111.0 11.0 22 0.0


The distance column indicates the minimum distance between the corresponding lat2/long2 and all the entries in lat1/long1. My code for the same is as follows:



import pandas as pd
from haversine import haversine
import numpy as np

path = 'distance.xlsx'
df = pd.read_excel(path, parse_cols = [1,2,4,5,7,8])
df = df.assign(dist=pd.Series(np.zeros(27055)).values);
df = df.assign(Depot=pd.Series(np.zeros(27055)).values);


temp = 1e10
for i in range(0, len(df)):
for j in range(0, len(df)):
if df.iloc[j]['lat1'] is '.' and df.iloc[j]['long1'] is '.':
continue
else:
p1 = (df.iloc[j]['lat1'],df.iloc[j]['long1'])
p2 = (df.iloc[i]['lat2'],df.iloc[i]['long2'])
df.dist.iloc[i] = min(temp,haversine(p1, p2, miles=True))
temp = df.dist.iloc[i]


temp = 1e10


For the minimum distance for each index, I want to know the corresponding p2 (lat1,long1) and store it in the Depot column. I understand this must be some indexing command but I'm struggling with the exact form.










share|improve this question

























  • Could you elaborate on the sentence "For the minimum distance for each index, I want to know the corresponding p2 (lat1,long1) and store it in the Depot column"? I didn't understand what you asked for :)

    – Gal Avineri
    Nov 19 '18 at 18:39














1












1








1








The head of my data as follows:



   lat1 long1 state           county   lat2  long2  dist Depot
0 . . AK Aleutians West 11.0 23.0 121 0.0
1 . . AK Wade Hampton 33.0 11.0 12 0.0
2 . . AK North Slope 55.0 11.0 43 0.0
3 . . AK Kenai Peninsula 44.0 11.0 43 0.0
4 . . AK Anchorage 11.0 11.0 99 0.0
5 1 2 AK Anchorage NaN NaN 1e10 0.0
6 . . AK Anchorage 55.0 44.0 33 0.0
7 3 4 AK Anchorage NaN NaN 1e10 0.0
8 . . AK Anchorage 3.0 2.0 23 0.0
9 . . AK Anchorage 5.0 11.0 32 0.0
10 . . AK Anchorage 42.0 22.0 33 0.0
11 . . AK Anchorage 11.0 2.0 33 0.0
12 . . AK Anchorage 444.0 1.0 43 0.0
13 . . AK Anchorage 1.0 2.0 53 0.0
14 0 2 AK Anchorage NaN NaN 1e10 0.0
15 . . AK Anchorage 1.0 1.0 32 0.0
16 . . AK Anchorage 111.0 11.0 22 0.0


The distance column indicates the minimum distance between the corresponding lat2/long2 and all the entries in lat1/long1. My code for the same is as follows:



import pandas as pd
from haversine import haversine
import numpy as np

path = 'distance.xlsx'
df = pd.read_excel(path, parse_cols = [1,2,4,5,7,8])
df = df.assign(dist=pd.Series(np.zeros(27055)).values);
df = df.assign(Depot=pd.Series(np.zeros(27055)).values);


temp = 1e10
for i in range(0, len(df)):
for j in range(0, len(df)):
if df.iloc[j]['lat1'] is '.' and df.iloc[j]['long1'] is '.':
continue
else:
p1 = (df.iloc[j]['lat1'],df.iloc[j]['long1'])
p2 = (df.iloc[i]['lat2'],df.iloc[i]['long2'])
df.dist.iloc[i] = min(temp,haversine(p1, p2, miles=True))
temp = df.dist.iloc[i]


temp = 1e10


For the minimum distance for each index, I want to know the corresponding p2 (lat1,long1) and store it in the Depot column. I understand this must be some indexing command but I'm struggling with the exact form.










share|improve this question
















The head of my data as follows:



   lat1 long1 state           county   lat2  long2  dist Depot
0 . . AK Aleutians West 11.0 23.0 121 0.0
1 . . AK Wade Hampton 33.0 11.0 12 0.0
2 . . AK North Slope 55.0 11.0 43 0.0
3 . . AK Kenai Peninsula 44.0 11.0 43 0.0
4 . . AK Anchorage 11.0 11.0 99 0.0
5 1 2 AK Anchorage NaN NaN 1e10 0.0
6 . . AK Anchorage 55.0 44.0 33 0.0
7 3 4 AK Anchorage NaN NaN 1e10 0.0
8 . . AK Anchorage 3.0 2.0 23 0.0
9 . . AK Anchorage 5.0 11.0 32 0.0
10 . . AK Anchorage 42.0 22.0 33 0.0
11 . . AK Anchorage 11.0 2.0 33 0.0
12 . . AK Anchorage 444.0 1.0 43 0.0
13 . . AK Anchorage 1.0 2.0 53 0.0
14 0 2 AK Anchorage NaN NaN 1e10 0.0
15 . . AK Anchorage 1.0 1.0 32 0.0
16 . . AK Anchorage 111.0 11.0 22 0.0


The distance column indicates the minimum distance between the corresponding lat2/long2 and all the entries in lat1/long1. My code for the same is as follows:



import pandas as pd
from haversine import haversine
import numpy as np

path = 'distance.xlsx'
df = pd.read_excel(path, parse_cols = [1,2,4,5,7,8])
df = df.assign(dist=pd.Series(np.zeros(27055)).values);
df = df.assign(Depot=pd.Series(np.zeros(27055)).values);


temp = 1e10
for i in range(0, len(df)):
for j in range(0, len(df)):
if df.iloc[j]['lat1'] is '.' and df.iloc[j]['long1'] is '.':
continue
else:
p1 = (df.iloc[j]['lat1'],df.iloc[j]['long1'])
p2 = (df.iloc[i]['lat2'],df.iloc[i]['long2'])
df.dist.iloc[i] = min(temp,haversine(p1, p2, miles=True))
temp = df.dist.iloc[i]


temp = 1e10


For the minimum distance for each index, I want to know the corresponding p2 (lat1,long1) and store it in the Depot column. I understand this must be some indexing command but I'm struggling with the exact form.







python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 17:39







db18

















asked Nov 19 '18 at 17:29









db18db18

1467




1467













  • Could you elaborate on the sentence "For the minimum distance for each index, I want to know the corresponding p2 (lat1,long1) and store it in the Depot column"? I didn't understand what you asked for :)

    – Gal Avineri
    Nov 19 '18 at 18:39



















  • Could you elaborate on the sentence "For the minimum distance for each index, I want to know the corresponding p2 (lat1,long1) and store it in the Depot column"? I didn't understand what you asked for :)

    – Gal Avineri
    Nov 19 '18 at 18:39

















Could you elaborate on the sentence "For the minimum distance for each index, I want to know the corresponding p2 (lat1,long1) and store it in the Depot column"? I didn't understand what you asked for :)

– Gal Avineri
Nov 19 '18 at 18:39





Could you elaborate on the sentence "For the minimum distance for each index, I want to know the corresponding p2 (lat1,long1) and store it in the Depot column"? I didn't understand what you asked for :)

– Gal Avineri
Nov 19 '18 at 18:39












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379818%2fgetting-index-of-minimum-distance-in-pandas-dataframe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379818%2fgetting-index-of-minimum-distance-in-pandas-dataframe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)