0

I've two dataframes which contain the following:

DF1: some data associated to some coordinates, n rows:

ID1  Lat1   Lon1
ID2  Lat2   Lon2
ID3  Lat3   Lon3
...
ID_n  Lat_n   Lon_n

DF2: some different data associated to another coordinates, s rows:

ID'1  Lat'1   Lon'1
ID'2  Lat'2   Lon'2
ID'3  Lat'3   Lon'3
...
ID'_s  Lat'_s   Lon'_s

Note that n!=s.

What I need to do is, using python/pandas, for DF1[ID1] calculate the distance between Lat1/Lon1 and ALL Lat'1...Lat's/Lon'1...Lon's and get the minimum value.

The calculus of the distance is not a problem, the problem is how i can implement the iterated loop. An example of what i need:

ID    N
1     5
2     3
3     6
4     9

ID'    N'
1      2
2      4
3      1

Result would be:

ID    Nmin
1     1 (5-4)
2    -1 (3-4)
3     2 (6-4)
4     5 (9-4)
webprogrammer
  • 2,393
  • 3
  • 21
  • 27

2 Answers2

0

It looks like you just should find the maximum value in DF2, and subtract it from all the values in DF1. That`s because if you are looking for the min value, you should subtract the maximum value.

So, you can just use:

DF1['N_min'] = DF1['N'] - DF2['N'].max()
E. Zeytinci
  • 2,642
  • 1
  • 20
  • 37
theletz
  • 1,713
  • 2
  • 16
  • 22
  • Not exactly... what i've to do is: calculate the distance between the coordinates in the first row in DF1 and all the coordinates in DF2, and get the minimum value. Then, calculate the distance between the coordinates in the second row in DF1 and all the coordinates in DF2, and get the minimum value. And iterate this for all the coordinates in DF1. Huge thanks! – Luismi Garay Teja Feb 09 '20 at 12:40
0

I'm a learner myself, but your question is a little unclear, what I understand is that you want the distance between coordinates, and the minimum value, now for for subracting both latitude and longitude values of one dataframe with the max latitude and longitude of the other, you want to iterate...so...here's what I tried out.

import pandas as pd
import numpy as np

df1 = {
    'ID':['A','B','C','D'],
    'LAT':[1,2,3,4],
    'LONG':[5,6,7,8],
}

df2 = {
    'ID':['E','F','G'],
    'LAT':[9,10,11],
    'LONG':[12,13,14],
}

df1 = pd.DataFrame(df1)
df2 = pd.DataFrame(df2)

output:

  ID  LAT  LONG
0  A    1     5
1  B    2     6
2  C    3     7
3  D    4     8
  ID  LAT  LONG
0  E    9    12
1  F   10    13
2  G   11    14

now for iterating I took a little help from the answer here:

for index, coor in df1.iterrows():
    print(coor['LAT'] - df2['LAT'].max(), coor['LONG'] - df2['LONG'].max())

output will be:

-10 -9
-9 -8
-8 -7
-7 -6

but I guess applying loops makes it complicated. you may use other ways for converting it to a dataframe again, still I hope this was of any help.

  • first of all, thanks for your answer. As I told the other mate, this is not exactly what I was meanin. I don't want to substract to the coordinates in DF1 the maximum coordinates in DF2. what i've to do is to calculate the distance between the coordinates in the first row in DF1 and all the coordinates in DF2, and get the minimum value. Then, calculate the distance between the coordinates in the second row in DF1 and all the coordinates in DF2, and get the minimum value. And iterate this for all the coordinates in DF1. Maybe i didn't explain it properly in the post... Huge thanks! – Luismi Garay Teja Feb 09 '20 at 13:35
  • np...you can use the distance formula in the for loop with this **.iterrows()** method i guess –  Feb 10 '20 at 15:32
  • if my answer was of any help kindly approve it, i'd appreciate it. ty –  Feb 10 '20 at 15:33