【问题标题】:Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions无法将列转换为布尔值:请使用 '&' 表示 'and'、'|' for 'or', '~' for 'not' 在构建 DataFrame 布尔表达式时
【发布时间】:2022-01-25 07:27:08
【问题描述】:
from pyspark.sql.window import Window
import mpu
from pyspark.sql.functions import udf
from pyspark.sql.functions import lag

    from math import sin, cos, sqrt, atan2
    windowSpec  = Window.partitionBy("UserID").orderBy(asc("Timestamp"))
    df14=df.withColumn("newLatitude",lag("Latitude",1).over(windowSpec)) \
          .withColumn("newLongitude",lag("Longitude",1).over(windowSpec)) \
          .drop('AllZero'," Date","Time","Altitude") 
    df15=df14.orderBy(col("UserID").asc(),col("Timestamp").asc())
    df16=df15.na.drop()
    from geopy.distance import geodesic
    origin = (30.172705, 31.526725)  # (latitude, longitude) don't confuse
    dist = (30.288281, 31.732326)
    print(geodesic(origin, dist).meters)
    df17=df16.withColumn("distance",geodesic((col("Latitude"), col("Longitude")), (col("newLatitude"), col("newLongitude"))).meters)
    df17.show()

我尝试使用延迟函数将前一组纬度和经度放在原始df之后,但是当我尝试计算这两组纬度和经度之间的距离时,它变得像:

/usr/local/spark/python/pyspark/sql/column.py in nonzero(self) 688 689 def 非零(自我): --> 690 raise ValueError("Cannot convert column to bool: please use '&' for 'and', '|' for 'or', " 第691章 692 bool = 非零

ValueError:无法将列转换为布尔值:请使用 '&' 表示 'and'、'|'在构建 DataFrame 布尔表达式时,为 'or','~' 为 'not'。 我真的不明白发生了什么。

【问题讨论】:

  • 这能回答你的问题吗? ValueError: Cannot convert column into bool
  • 对不起,我看到了那个页面,那里的答案无法解决我的问题,但我有些方法设法用不同的答案解决它,我将在下面发布

标签: pyspark


【解决方案1】:
def dist_col(a, b, c, d):
    col_dist = geodesic((a,b), (c,d)).meters
    return col_dist
# integer datatype is defined
new_f = F.udf(dist_col, FloatType())

df17=df16.withColumn('dist', new_f(col("Latitude"), col("Longitude"),col("newLatitude"), col("newLongitude")))

我在withColumn函数之外创建了一个计算函数,并使用udf定义参数类型。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-08
  • 1970-01-01
  • 2021-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多