在数据框中应用Python-Geohash编码函数

7
我正在尝试将编码函数应用于数据框。但是我一直遇到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')
>>> 

输入单个数值对:

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

显示 gh.encode 正常工作。

还有其他方法吗?

1个回答

12

在 apply 语句中,应使用 x 的值而非 df 的值:

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

这将得出:

>>> 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接