如何将火花数据框中的两列相乘

akh*_*lly 0 apache-spark pyspark

假设我有一个名为“orderitems”的数据框,其架构如下

    DataFrame[order_item_id: int, order_item_order_id: int, order_item_product_id: int, order_item_quantity: int, order_item_subtotal: float, order_item_product_price: float]
Run Code Online (Sandbox Code Playgroud)

因此,作为检查数据质量的一部分,我需要确保所有行都满足以下公式: order_item_subtotal = (order_item_quantity*order_item_product_price)。为此,我需要添加一个名为“valid”的单独列,对于满足上述公式的所有行,它应该将“Y”作为值,而对于所有其他行,它应该将“N”作为值。我决定使用 when() 和 else() 以及 withColumn() 方法,如下所示。

    orderitems.withColumn("valid",when(orderitems.order_item_subtotal != (orderitems.order_item_product_price * orderitems.order_item_quantity),'N').otherwise("Y"))
Run Code Online (Sandbox Code Playgroud)

但它返回给我以下错误:

    TypeError: 'Column' object is not callable
Run Code Online (Sandbox Code Playgroud)

我知道这是因为我试图将两个列对象相乘。但我不确定如何解决这个问题,因为我仍然在 spark 学习过程中。我想知道,如何解决这个问题。我在 Python 中使用 Spark 2.3.0

1pl*_*ara 5

尝试这样的事情:

from pyspark.sql.functions import col,when
orderitems.withColumn("valid",
          when(col("order_item_subtotal") != (col("order_item_product_price") * col("order_item_quantity")),"N")
          .otherwise("Y")).show()
Run Code Online (Sandbox Code Playgroud)