1

I am trying to apply this code:

import h3

coords_1 = (52.2296756, 21.0122287)
coords_2 = (52.406374, 16.9251681)
distance = h3.point_dist(coords_1, coords_2, unit='km')
distance

to a pandas dataframe. This is my not working attempt.

data = {'lat1':[52.2296756],
        'long1':[21.0122287],
        'lat2':[52.406374],
        'long2':[16.9251681],      
}
df = pd.DataFrame(data)
df

df['distance'] = = h3.point_dist((df['lat1'], df['long1']), (df['lat2'], df['long2']), unit='km')

Any help would be very much appreciated. Thanks!

cs0815
  • 16,751
  • 45
  • 136
  • 299
  • 1
    Does this answer your question? [Pandas Latitude-Longitude to distance between successive rows](https://stackoverflow.com/questions/40452759/pandas-latitude-longitude-to-distance-between-successive-rows) – Yaakov Bressler Mar 28 '22 at 14:35
  • 1
    no - I googled this - I am not after successive rows! – cs0815 Mar 28 '22 at 14:36
  • emm just look at me data frame example?! – cs0815 Mar 28 '22 at 14:40
  • Explicit > implicit... – Yaakov Bressler Mar 28 '22 at 14:42
  • What does "not working attempt" mean? What happens when you run your code? Do you get an error? If so what is the error? Did you try to google the error message to see what solutions others have found? If you don't get any errors, what is the output? How does it differ from what you actually want instead? Please [edit] your question to include these details. – Code-Apprentice Mar 28 '22 at 14:57
  • wow a lot of pointless/unfriendly messages. this is not a duplicate as already outlined in the comments!!! – cs0815 Mar 28 '22 at 14:58
  • 1
    @cs0815 How are these comments unfriendly? We are trying to help. However, your question doesn't include enough details to do so. – Code-Apprentice Mar 28 '22 at 14:59
  • There is a not working example - and not working means not working it does not execute in this case. And no it is not a duplicate! – cs0815 Mar 28 '22 at 15:01
  • 1
    @cs0815 Just repeating words doesn't explain what you mean. Your question does not include enough details to understand what the actual problem is. Please read [mcve] and [ask] for tips on how to ask a question that attracts the answers you are looking for. I've even included specific questions for you to answer. Just the error message you get will go a long way to understanding what the actual problem is. – Code-Apprentice Mar 28 '22 at 15:03
  • I am sorry I just noticed the == - how stupid. sorry I did not have much sleep last night. – cs0815 Mar 28 '22 at 15:09
  • @cs0815 For future reference, please include the error message in your question. – Code-Apprentice Mar 28 '22 at 17:08

2 Answers2

5

Assuming you have more than a single row for which you would like to compute the distance you can use apply as follows:

df['Dist'] = df.apply(lambda row: h3.point_dist((row['lat1'], row['long1']), (row['lat2'], row['long2'])), axis=1)

Which will add a column to your dataframe simi9lar to the following:

      lat1        long1      lat2       long2        Dist
0   52.229676   21.012229   52.406374   16.925168   2.796556
1   57.229676   30.001176   48.421365   17.256314   6.565542 

Please note, my distance calculations may not agree with yours, since I used a dummy function for h3.point_dist computation

itprorh66
  • 3,110
  • 4
  • 9
  • 21
2

It's working you need to just delete the second "="

data = {'lat1':[52.2296756],
        'long1':[21.0122287],
        'lat2':[52.406374],
        'long2':[16.9251681],      
}
df = pd.DataFrame(data)
df

df['distance'] =  h3.point_dist((df['lat1'], df['long1']), (df['lat2'], df['long2']), unit='km')

result

Ran A
  • 746
  • 3
  • 7
  • 19