2017-12-02 5 views
0
import pygeohash as pgh 

pgh.encode(45,55) 

'tpzpgxczbzur' 

上記の手順は素晴らしいです。私は以下のデータフレームを作成しようとしています。スパークデータフレーム列をgeohash関数 - pysparkに渡します。列をboolに変換できません:

l = [(45,25),(75,22),(85,20),(89,26)] 

rdd = sc.parallelize(l) 
geoCords = rdd.map(lambda x: Row(lat=x[0], long=int(x[1]))) 
geoCordsSchema = sqlContext.createDataFrame(geoCords) 
geoCordsSchema.show() 

+---+----+ 
|lat|long| 
+---+----+ 
| 45| 25| 
| 75| 22| 
| 85| 20| 
| 89| 26| 
+---+----+ 

これで、スパークデータフレームが正常に作成されます。今私はPygeohashエンコードを使用して、以下のようにエラーを投げている:

pgh.encode(geoCordsSchema.lat, geoCordsSchema.long, precision = 7) 

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/Library/Python/2.7/site-packages/pygeohash/geohash.py", line 96, in encode 
    if longitude > mid: 
    File "/usr/local/spark/python/pyspark/sql/column.py", line 427, in __nonzero__ 
    raise ValueError("Cannot convert column into bool: please use '&' for 'and', '|' for 'or', " 
ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions. 

答えて

0

あなたはそれを変換するために、いくつかの機能に直接列を使用することはできません。あなたはそれを達成するためにUDFを使うことができます、

from pyspark.sql import function as F 
udf1 = F.udf(lambda x,y: pgh.encode(x,y,precision=7)) 
geoCordsSchema.select('lat','long',udf1('lat','long').alias('encodedVal')).show() 
+---+----+-----------+ 
|lat|long|encodedeVal| 
+---+----+-----------+ 
| 45| 25| sxczbzu| 
| 75| 22| umrdst7| 
| 85| 20| urn5x1g| 
| 89| 26| uxf6r9u| 
+---+----+-----------+ 
関連する問題