Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying the python-geohash encode function on a dataframe

I am trying to apply the encode function to a dataframe. I keep meeting a ValueError:

>>> import pandas as pd
>>> import pygeohash as gh
>>> data = { 'latitude': [4.123, 24.345, 31.654],  'longitude': [25.432, 4.234, 57.098]}
>>> df = pd.DataFrame(data)
>>> df
   latitude  longitude
0     4.123     25.432
1    24.345      4.234
2    31.654     57.098
>>> df['geohash']=df.apply(lambda x: gh.encode(df.latitude, df.longitude, precision=5), axis=1)

Traceback (most recent call last):
  .........
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 0')
>>> 

Putting in a single pair of values:

>>> gh.encode(22,36, precision = 5)
'sgct5'

shows that gh.encode is working.

Is there any other way of doing this?

like image 812
Goomer Avatar asked Aug 03 '17 13:08

Goomer


People also ask

What is Geohash in Python?

Geohash is a string representation of two dimensional geometric coordinates. This technique is well described at Wikipedia Geohash. It is basically, a form of Z-order curve. Quadtree can be used to construct a string representation. In this library, 0 for SW, 1 for SE, 2 for NW and 3 for NE.

What are Geohash coordinates?

Geohashing is a geocoding method used to encode geographic coordinates (latitude and longitude) into a short string of digits and letters delineating an area on a map, which is called a cell, with varying resolutions. The more characters in the string, the more precise the location.


1 Answers

You should use the values of x, not of df in the apply statement:

df['geohash']=df.apply(lambda x: gh.encode(x.latitude, x.longitude, precision=5), axis=1)
#                                          ^           ^  use x

This yields:

>>> df['geohash']=df.apply(lambda x: gh.encode(x.latitude, x.longitude, precision=5), axis=1)
>>> df
   latitude  longitude geohash
0     4.123     25.432   s8dp6
1    24.345      4.234   sh742
2    31.654     57.098   tm8s5
like image 106
Willem Van Onsem Avatar answered Oct 04 '22 16:10

Willem Van Onsem