Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying a geohash_decode function to a column in a dataframe

Well, I geohash_encoded the geographical coordinates to geohashes. My aim is to calculate the distance with some level of accuracy. I am trying to geohash_decode the geohashes back to geographical coordinates but I have failed to come up with a function that can do that to a column in a dataframe

like image 454
Winnie Okuta Avatar asked Feb 01 '26 13:02

Winnie Okuta


1 Answers

Assuming:

  • you are asking about Python (apologies if this was an R, Scala or other dataframe question, but you didn't specify )
  • you have a Python pandas DataFrame object df
  • df has a column named geohash containing your geohashes
  • you have the geohash2 library installed and imported (this may work with other Geohash libraries...)
  • you want to overwrite df with a new DataFrame containing all the old data plus the new latitude and longitude columns

The following should work:

def gh_decode(hash):
    lat, lon = geohash2.decode(hash)
    return pd.Series({"latitude":lat, "longitude":lon})

df = df.join(df["geohash"].apply(gh_decode))
like image 144
Alnilam Avatar answered Feb 04 '26 05:02

Alnilam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!