为什么 pyspark 中数据框的最大值不正确?
Why the max value is not correct from dataframe in pyspark?
我有一个名为 orders 的数据框,从 csv 文件加载,而 days_since_prior_order 列有一些空白值。
orders = spark.read.csv("/Users/yanan.chen/Downloads/instacart/orders.csv",header=True)
orders.createOrReplaceTempView("orders")
spark.sql("select * from orders limit 30").show()
+--------+-------+--------+------------+---------+-----------------+----------------------+
|order_id|user_id|eval_set|order_number|order_dow|order_hour_of_day|days_since_prior_order|
+--------+-------+--------+------------+---------+-----------------+----------------------+
| 2539329| 1| prior| 1| 2| 08| |
| 2398795| 1| prior| 2| 3| 07| 15.0|
| 473747| 1| prior| 3| 3| 12| 21.0|
| 2254736| 1| prior| 4| 4| 07| 29.0|
| 431534| 1| prior| 5| 4| 15| 28.0|
| 3367565| 1| prior| 6| 2| 07| 19.0|
| 550135| 1| prior| 7| 1| 09| 20.0|
| 3108588| 1| prior| 8| 1| 14| 14.0|
| 2295261| 1| prior| 9| 1| 16| 0.0|
| 2550362| 1| prior| 10| 4| 08| 30.0|
| 1187899| 1| train| 11| 4| 08| 14.0|
| 2168274| 2| prior| 1| 2| 11| |
| 1501582| 2| prior| 2| 5| 10| 10.0|
| 1901567| 2| prior| 3| 1| 10| 3.0|
| 738281| 2| prior| 4| 2| 10| 8.0|
| 1673511| 2| prior| 5| 3| 11| 8.0|
| 1199898| 2| prior| 6| 2| 09| 13.0|
| 3194192| 2| prior| 7| 2| 12| 14.0|
| 788338| 2| prior| 8| 1| 15| 27.0|
| 1718559| 2| prior| 9| 2| 09| 8.0|
+--------+-------+--------+------------+---------+-----------------+----------------------+
如您所见,days_since_prior_order
中有一些空白,实际上是''。 spark.sql("select * from orders where days_since_prior_order <> '' ").show()
+--------+-------+--------+------------+---------+-----------------+----------------------+
|order_id|user_id|eval_set|order_number|order_dow|order_hour_of_day|days_since_prior_order|
+--------+-------+--------+------------+---------+-----------------+----------------------+
| 2398795| 1| prior| 2| 3| 07| 15.0|
| 473747| 1| prior| 3| 3| 12| 21.0|
| 2254736| 1| prior| 4| 4| 07| 29.0|
| 431534| 1| prior| 5| 4| 15| 28.0|
| 3367565| 1| prior| 6| 2| 07| 19.0|
| 550135| 1| prior| 7| 1| 09| 20.0|
| 3108588| 1| prior| 8| 1| 14| 14.0|
| 2295261| 1| prior| 9| 1| 16| 0.0|
| 2550362| 1| prior| 10| 4| 08| 30.0|
| 1187899| 1| train| 11| 4| 08| 14.0|
| 1501582| 2| prior| 2| 5| 10| 10.0|
| 1901567| 2| prior| 3| 1| 10| 3.0|
| 738281| 2| prior| 4| 2| 10| 8.0|
| 1673511| 2| prior| 5| 3| 11| 8.0|
| 1199898| 2| prior| 6| 2| 09| 13.0|
| 3194192| 2| prior| 7| 2| 12| 14.0|
| 788338| 2| prior| 8| 1| 15| 27.0|
| 1718559| 2| prior| 9| 2| 09| 8.0|
| 1447487| 2| prior| 10| 1| 11| 6.0|
| 1402090| 2| prior| 11| 1| 10| 30.0|
+--------+-------+--------+------------+---------+-----------------+----------------------+
但是,令我困惑的是,当我执行spark.sql("select min(days_since_prior_order), max(days_since_prior_order) from orders where days_since_prior_order <> '' ").show()
时,结果中的最大值不正确。
+---------------------------+---------------------------+
|min(days_since_prior_order)|max(days_since_prior_order)|
+---------------------------+---------------------------+
| 0.0| 9.0|
+---------------------------+---------------------------+
我的代码有什么问题?
您需要将该列从字符串类型转换为数字类型。
做类似的事情:
from pyspark.sql.functions import col
orders = orders.withColumn('days_since_prior_order',
col('days_since_prior_order').cast('double'))
那么你会得到正确的结果。
另一种方法是使用 udf(用户定义的函数,但当我们简化时为什么要复杂。)
我有一个名为 orders 的数据框,从 csv 文件加载,而 days_since_prior_order 列有一些空白值。
orders = spark.read.csv("/Users/yanan.chen/Downloads/instacart/orders.csv",header=True)
orders.createOrReplaceTempView("orders")
spark.sql("select * from orders limit 30").show()
+--------+-------+--------+------------+---------+-----------------+----------------------+
|order_id|user_id|eval_set|order_number|order_dow|order_hour_of_day|days_since_prior_order|
+--------+-------+--------+------------+---------+-----------------+----------------------+
| 2539329| 1| prior| 1| 2| 08| |
| 2398795| 1| prior| 2| 3| 07| 15.0|
| 473747| 1| prior| 3| 3| 12| 21.0|
| 2254736| 1| prior| 4| 4| 07| 29.0|
| 431534| 1| prior| 5| 4| 15| 28.0|
| 3367565| 1| prior| 6| 2| 07| 19.0|
| 550135| 1| prior| 7| 1| 09| 20.0|
| 3108588| 1| prior| 8| 1| 14| 14.0|
| 2295261| 1| prior| 9| 1| 16| 0.0|
| 2550362| 1| prior| 10| 4| 08| 30.0|
| 1187899| 1| train| 11| 4| 08| 14.0|
| 2168274| 2| prior| 1| 2| 11| |
| 1501582| 2| prior| 2| 5| 10| 10.0|
| 1901567| 2| prior| 3| 1| 10| 3.0|
| 738281| 2| prior| 4| 2| 10| 8.0|
| 1673511| 2| prior| 5| 3| 11| 8.0|
| 1199898| 2| prior| 6| 2| 09| 13.0|
| 3194192| 2| prior| 7| 2| 12| 14.0|
| 788338| 2| prior| 8| 1| 15| 27.0|
| 1718559| 2| prior| 9| 2| 09| 8.0|
+--------+-------+--------+------------+---------+-----------------+----------------------+
如您所见,days_since_prior_order
中有一些空白,实际上是''。 spark.sql("select * from orders where days_since_prior_order <> '' ").show()
+--------+-------+--------+------------+---------+-----------------+----------------------+
|order_id|user_id|eval_set|order_number|order_dow|order_hour_of_day|days_since_prior_order|
+--------+-------+--------+------------+---------+-----------------+----------------------+
| 2398795| 1| prior| 2| 3| 07| 15.0|
| 473747| 1| prior| 3| 3| 12| 21.0|
| 2254736| 1| prior| 4| 4| 07| 29.0|
| 431534| 1| prior| 5| 4| 15| 28.0|
| 3367565| 1| prior| 6| 2| 07| 19.0|
| 550135| 1| prior| 7| 1| 09| 20.0|
| 3108588| 1| prior| 8| 1| 14| 14.0|
| 2295261| 1| prior| 9| 1| 16| 0.0|
| 2550362| 1| prior| 10| 4| 08| 30.0|
| 1187899| 1| train| 11| 4| 08| 14.0|
| 1501582| 2| prior| 2| 5| 10| 10.0|
| 1901567| 2| prior| 3| 1| 10| 3.0|
| 738281| 2| prior| 4| 2| 10| 8.0|
| 1673511| 2| prior| 5| 3| 11| 8.0|
| 1199898| 2| prior| 6| 2| 09| 13.0|
| 3194192| 2| prior| 7| 2| 12| 14.0|
| 788338| 2| prior| 8| 1| 15| 27.0|
| 1718559| 2| prior| 9| 2| 09| 8.0|
| 1447487| 2| prior| 10| 1| 11| 6.0|
| 1402090| 2| prior| 11| 1| 10| 30.0|
+--------+-------+--------+------------+---------+-----------------+----------------------+
但是,令我困惑的是,当我执行spark.sql("select min(days_since_prior_order), max(days_since_prior_order) from orders where days_since_prior_order <> '' ").show()
时,结果中的最大值不正确。
+---------------------------+---------------------------+
|min(days_since_prior_order)|max(days_since_prior_order)|
+---------------------------+---------------------------+
| 0.0| 9.0|
+---------------------------+---------------------------+
我的代码有什么问题?
您需要将该列从字符串类型转换为数字类型。 做类似的事情:
from pyspark.sql.functions import col
orders = orders.withColumn('days_since_prior_order',
col('days_since_prior_order').cast('double'))
那么你会得到正确的结果。
另一种方法是使用 udf(用户定义的函数,但当我们简化时为什么要复杂。)