"any value" 的 PySpark 聚合函数
PySpark aggregation function for "any value"
我有一个带有 A
字段的 PySpark Dataframe,几个 B
字段依赖于 A
(A->B
) 和 C
我的字段想要对每个 A 进行聚合。例如:
A | B | C
----------
A | 1 | 6
A | 1 | 7
B | 2 | 8
B | 2 | 4
我希望按 A
分组,在 C
.[=27 上呈现任何 B
和 运行 聚合(比方说 SUM
) =]
预期结果为:
A | B | C
----------
A | 1 | 13
B | 2 | 12
SQL-明智的我会这样做:
SELECT A, COALESCE(B) as B, SUM(C) as C
FROM T
GROUP BY A
PySpark 的方法是什么?
我可以按 A 和 B 分组,或者按每个 A select MIN(B)
,例如:
df.groupBy('A').agg(F.min('B').alias('B'),F.sum('C').alias('C'))
或
df.groupBy(['A','B']).agg(F.sum('C').alias('C'))
但这似乎效率不高。 PySpark 中是否有类似于 SQL coalesce
的内容?
谢谢
您只需要使用 first
即可:
from pyspark.sql.functions import first, sum, col
from pyspark.sql import Row
array = [Row(A="A", B=1, C=6),
Row(A="A", B=1, C=7),
Row(A="B", B=2, C=8),
Row(A="B", B=2, C=4)]
df = sqlContext.createDataFrame(sc.parallelize(array))
results = df.groupBy(col("A")).agg(first(col("B")).alias("B"), sum(col("C")).alias("C"))
现在让我们检查一下结果:
results.show()
# +---+---+---+
# | A| B| C|
# +---+---+---+
# | B| 2| 12|
# | A| 1| 13|
# +---+---+---+
来自评论:
Is first
here is computationally equivalent to any
?
groupBy
导致随机播放。因此,非确定性行为是可以预期的。
first
的文档中确认了这一点:
Aggregate function: returns the first value in a group.
The function by default returns the first values it sees. It will return the first non-null value it sees when ignoreNulls is set to true. If all values are null, then null is returned.
note:: The function is non-deterministic because its results depends on order of rows which may be non-deterministic after a shuffle.
所以是的,在计算上是相同的,如果您需要确定性行为,这就是您需要使用排序的原因之一。
希望对您有所帮助!
我有一个带有 A
字段的 PySpark Dataframe,几个 B
字段依赖于 A
(A->B
) 和 C
我的字段想要对每个 A 进行聚合。例如:
A | B | C
----------
A | 1 | 6
A | 1 | 7
B | 2 | 8
B | 2 | 4
我希望按 A
分组,在 C
.[=27 上呈现任何 B
和 运行 聚合(比方说 SUM
) =]
预期结果为:
A | B | C
----------
A | 1 | 13
B | 2 | 12
SQL-明智的我会这样做:
SELECT A, COALESCE(B) as B, SUM(C) as C
FROM T
GROUP BY A
PySpark 的方法是什么?
我可以按 A 和 B 分组,或者按每个 A select MIN(B)
,例如:
df.groupBy('A').agg(F.min('B').alias('B'),F.sum('C').alias('C'))
或
df.groupBy(['A','B']).agg(F.sum('C').alias('C'))
但这似乎效率不高。 PySpark 中是否有类似于 SQL coalesce
的内容?
谢谢
您只需要使用 first
即可:
from pyspark.sql.functions import first, sum, col
from pyspark.sql import Row
array = [Row(A="A", B=1, C=6),
Row(A="A", B=1, C=7),
Row(A="B", B=2, C=8),
Row(A="B", B=2, C=4)]
df = sqlContext.createDataFrame(sc.parallelize(array))
results = df.groupBy(col("A")).agg(first(col("B")).alias("B"), sum(col("C")).alias("C"))
现在让我们检查一下结果:
results.show()
# +---+---+---+
# | A| B| C|
# +---+---+---+
# | B| 2| 12|
# | A| 1| 13|
# +---+---+---+
来自评论:
Is
first
here is computationally equivalent toany
?
groupBy
导致随机播放。因此,非确定性行为是可以预期的。
first
的文档中确认了这一点:
Aggregate function: returns the first value in a group. The function by default returns the first values it sees. It will return the first non-null value it sees when ignoreNulls is set to true. If all values are null, then null is returned. note:: The function is non-deterministic because its results depends on order of rows which may be non-deterministic after a shuffle.
所以是的,在计算上是相同的,如果您需要确定性行为,这就是您需要使用排序的原因之一。
希望对您有所帮助!