2017-08-03 2 views
1

Ich versuche, die Codierungsfunktion auf einen Datenrahmen anzuwenden. Ich halte Treffen ein Valueerror:Anwenden der Python-Geohash-Codierungsfunktion auf einen Datenrahmen

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

in einem einzigen Paar von Werten Putting:

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

zeigt, dass gh.encode arbeitet.

Gibt es eine andere Möglichkeit, dies zu tun?

Antwort

0

Sie sollten die Werte von x, nicht von df in der Anwendung Anweisung:

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

Dies ergibt:

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