我有一个Spark数据帧,其中的列中包含一些@Html.ActionLink(
"Login", "AutoLogin", "Account", routeValues: new{}, htmlAttributes: new {id = "loginLink"})
值。我需要计算非空值之前的连续null
值。
我将使用null
做这样的事情(该代码并未针对numpy进行优化,因为我正试图在问题中不使用它):
numpy
这样输出将如下所示:
import numpy as np
x = np.array([[0, None], [1, 3.], [2, 7.], [3, None], [4, 4.], [5, 3.],
[6, None], [7, None], [8, 5.], [9, 2.], [10, None]])
def nan_count(l, n):
assert n <= len(l) + 1
assert n >= 0
if n < 1 or l[n-1] is not None:
return 0
return nan_count(l, n-1) + 1
y = map(lambda i: nan_count(x[:,1], i), x[:,0])
res = np.concatenate([x, np.asarray(y).reshape(-1,1)], axis = 1)
res
现在,如果我有一个Out[31]: [0, 1, 0, 0, 1, 0, 0, 1, 2, 0, 0]
这样的Spark DataFrame:
x
x = sc.parallelize([[0, None], [1, 3.], [2, 7.], [3, None], [4, 4.],
[5, 3.], [6, None], [7, None], [8, 5.], [9, 2.], [10, None]])\
.toDF()
x.show()
我如何获得相同的输出?
我已经使用+---+----+
| _1| _2|
+---+----+
| 0|null|
| 1| 3.0|
| 2| 7.0|
| 3|null|
| 4| 4.0|
| 5| 3.0|
| 6|null|
| 7|null|
| 8| 5.0|
| 9| 2.0|
| 10|null|
+---+----+
尝试了一些工作方法,但是在选择一个值之前我就遇到了问题(我尝试使用udf
和select
filter
方法在udf中,但这是不允许的。
编辑:我不知道可能会找到多少个连续的pyspark.sql.dataframe.DataFrame
。
答案 0 :(得分:1)
我在代码中添加了注释,以解释每个步骤,直到达到所需的输出为止。
当然,没有必要从下面的示例创建所有列,并且可能可以对这段代码进行很多改进,但是我认为这很重要,它一步一步地向您展示并初步开始解决您的问题。
x = sc.parallelize([
[0, None],
[1, 3.],
[2, 7.],
[3, None],
[4, 4.],
[5, 3.],
[6, None],
[7, None],
[8, 5.],
[9, 2.],
[10, None]
])
# Assigned values in columns A and B to facilitate manipulation
x = x.toDF(['A', 'B'])
# Prints initial DF
x.show()
输出:
+---+----+
| A| B|
+---+----+
| 0|null|
| 1| 3.0|
| 2| 7.0|
| 3|null|
| 4| 4.0|
| 5| 3.0|
| 6|null|
| 7|null|
| 8| 5.0|
| 9| 2.0|
| 10|null|
+---+----+
# Transform null values into "1"
x = x.withColumn('C', when(x.B.isNull(), 1))
x.show()
输出:
+---+----+----+
| A| B| C|
+---+----+----+
| 0|null| 1|
| 1| 3.0|null|
| 2| 7.0|null|
| 3|null| 1|
| 4| 4.0|null|
| 5| 3.0|null|
| 6|null| 1|
| 7|null| 1|
| 8| 5.0|null|
| 9| 2.0|null|
| 10|null| 1|
+---+----+----+
# Creates a spec that order column A
order_spec = Window().orderBy('A')
# Doing a cumulative sum. See the explanation
# https://stackoverflow.com/questions/56384625/pyspark-cumulative-sum-with-reset-condition
x = x \
.withColumn('tmp', sum((x.C.isNull()).cast('int')).over(order_spec)) \
.withColumn('D', sum(x.C).over(order_spec.partitionBy("tmp"))) \
.drop('tmp')
x.show()
输出:
+---+----+----+----+
| A| B| C| D|
+---+----+----+----+
| 0|null| 1| 1|
| 1| 3.0|null|null|
| 2| 7.0|null|null|
| 3|null| 1| 1|
| 4| 4.0|null|null|
| 5| 3.0|null|null|
| 6|null| 1| 1|
| 7|null| 1| 2|
| 8|null| 1| 3|
| 9| 5.0|null|null|
| 10| 2.0|null|null|
| 11|null| 1| 1|
+---+----+----+----+
# Put values from column D to one row above and select the desired output values
x = x.withColumn('E', lag(x.D, ).over(order_spec)) \
.select(x.A, x.B, when(col('E').isNotNull(), col('E')).otherwise(0).alias('nan_count'))
x.show()
输出:
+---+----+---------+
| A| B|nan_count|
+---+----+---------+
| 0|null| 0|
| 1| 3.0| 1|
| 2| 7.0| 0|
| 3|null| 0|
| 4| 4.0| 1|
| 5| 3.0| 0|
| 6|null| 0|
| 7|null| 1|
| 8|null| 2|
| 9| 5.0| 3|
| 10| 2.0| 0|
| 11|null| 0|
+---+----+---------+
完整代码:
from pyspark.shell import sc
from pyspark.sql import Window
from pyspark.sql.functions import lag, when, sum, col
x = sc.parallelize([
[0, None], [1, 3.], [2, 7.], [3, None], [4, 4.],
[5, 3.], [6, None], [7, None], [8, None], [9, 5.], [10, 2.], [11, None]])
x = x.toDF(['A', 'B'])
# Transform null values into "1"
x = x.withColumn('C', when(x.B.isNull(), 1))
# Creates a spec that order column A
order_spec = Window().orderBy('A')
# Doing a cumulative sum with reset condition. See the explanation
# https://stackoverflow.com/questions/56384625/pyspark-cumulative-sum-with-reset-condition
x = x \
.withColumn('tmp', sum((x.C.isNull()).cast('int')).over(order_spec)) \
.withColumn('D', sum(x.C).over(order_spec.partitionBy("tmp"))) \
.drop('tmp')
# Put values from column D to one row above and select the desired output values
x = x.withColumn('E', lag(x.D, ).over(order_spec)) \
.select(x.A, x.B, when(col('E').isNotNull(), col('E')).otherwise(0).alias('nan_count'))
x.show()