pandas 在数据帧上应用 python-geohash 编码函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45485589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Applying the python-geohash encode function on a dataframe
提问by Goomer
I am trying to apply the encode function to a dataframe. I keep meeting a ValueError:
我正在尝试将编码功能应用于数据帧。我不断遇到 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.
表明 gh.encode 正在工作。
Is there any other way of doing this?
有没有其他方法可以做到这一点?
回答by Willem Van Onsem
You should use the values of x
, not of df
in the apply statement:
你应该在 apply 语句中使用 的值x
,而不是df
:
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