Gar*_*y C 5 python aggregate dataframe melt pyspark
正如主题所描述的,我有一个 PySpark 数据框,我需要将三列融合成行。每列基本上代表一个类别中的一个事实。最终目标是将数据聚合到每个类别的单个总数中。
这个数据帧中有数千万行,所以我需要一种方法来在 Spark 集群上进行转换而不将任何数据带回驱动程序(在这种情况下为 Jupyter)。
这是我的几个商店的数据框的摘录:
+-----------+----------------+-----------------+----------------+
| store_id |qty_on_hand_milk|qty_on_hand_bread|qty_on_hand_eggs|
+-----------+----------------+-----------------+----------------+
| 100| 30| 105| 35|
| 200| 55| 85| 65|
| 300| 20| 125| 90|
+-----------+----------------+-----------------+----------------+
这是所需的结果数据帧,每个商店多行,其中原始数据帧的列已融合到新数据帧的行中,每个原始列在新类别列中占一行:
+-----------+--------+-----------+
| product_id|CATEGORY|qty_on_hand|
+-----------+--------+-----------+
| 100| milk| 30|
| 100| bread| 105|
| 100| eggs| 35|
| 200| milk| 55|
| 200| bread| 85|
| 200| eggs| 65|
| 300| milk| 20|
| 300| bread| 125|
| 300| eggs| 90|
+-----------+--------+-----------+
最终,我想聚合结果数据框以获得每个类别的总数:
+--------+-----------------+
|CATEGORY|total_qty_on_hand|
+--------+-----------------+
| milk| 105|
| bread| 315|
| eggs| 190|
+--------+-----------------+
更新:有人建议这个问题是重复的,可以在这里回答。情况并非如此,因为该解决方案将行转换为列,而我需要做相反的事情,将列融合为行。
我认为你应该使用arrayandexplode来做到这一点,你不需要任何带有 UDF 或自定义函数的复杂逻辑。
array会将列合并为单个列,或对列进行注释。
explode会将数组列转换为一组行。
您需要做的就是:
df = (
df.withColumn('labels', F.explode( # <-- Split into rows
F.array( # <-- Combine columns
F.array(F.lit('milk'), F.col('qty_on_hand_milk')), # <-- Annotate column
F.array(F.lit('bread'), F.col('qty_on_hand_bread')),
F.array(F.lit('eggs'), F.col('qty_on_hand_eggs')),
)
))
.withColumn('CATEGORY', F.col('labels')[0])
.withColumn('qty_on_hand', F.col('labels')[1])
).select('store_id', 'CATEGORY', 'qty_on_hand')
Run Code Online (Sandbox Code Playgroud)
请注意如何简单地使用 ; 提取数组列的元素col('foo')[INDEX]。没有具体需要将它们分成单独的列。
这种方法对于不同的数据类型也很强大,因为它不会尝试在每一行上强制使用相同的模式(与使用结构不同)。
例如。如果 'qty_on_hand_bread' 是一个字符串,这仍然有效,结果模式将是:
root
|-- store_id: long (nullable = false)
|-- CATEGORY: string (nullable = true)
|-- qty_on_hand: string (nullable = true) <-- Picks best schema on the fly
Run Code Online (Sandbox Code Playgroud)
这是相同的代码,一步一步地使这里发生的事情变得显而易见:
import databricks.koalas as ks
import pyspark.sql.functions as F
# You don't need koalas, it's just less verbose for adhoc dataframes
df = ks.DataFrame({
"store_id": [100, 200, 300],
"qty_on_hand_milk": [30, 55, 20],
"qty_on_hand_bread": [105, 85, 125],
"qty_on_hand_eggs": [35, 65, 90],
}).to_spark()
df.show()
# Annotate each column with your custom label per row. ie. v -> ['label', v]
df = df.withColumn('label1', F.array(F.lit('milk'), F.col('qty_on_hand_milk')))
df = df.withColumn('label2', F.array(F.lit('bread'), F.col('qty_on_hand_bread')))
df = df.withColumn('label3', F.array(F.lit('eggs'), F.col('qty_on_hand_eggs')))
df.show()
# Create a new column which combines the labeled values in a single column
df = df.withColumn('labels', F.array('label1', 'label2', 'label3'))
df.show()
# Split into individual rows
df = df.withColumn('labels', F.explode('labels'))
df.show()
# You can now do whatever you want with your labelled rows, eg. split them into new columns
df = df.withColumn('CATEGORY', F.col('labels')[0])
df = df.withColumn('qty_on_hand', F.col('labels')[1])
df.show()
Run Code Online (Sandbox Code Playgroud)
...以及每个步骤的输出:
|store_id|qty_on_hand_milk|qty_on_hand_bread|qty_on_hand_eggs|
+--------+----------------+-----------------+----------------+
| 100| 30| 105| 35|
| 200| 55| 85| 65|
| 300| 20| 125| 90|
+--------+----------------+-----------------+----------------+
+--------+----------------+-----------------+----------------+----------+------------+----------+
|store_id|qty_on_hand_milk|qty_on_hand_bread|qty_on_hand_eggs| label1| label2| label3|
+--------+----------------+-----------------+----------------+----------+------------+----------+
| 100| 30| 105| 35|[milk, 30]|[bread, 105]|[eggs, 35]|
| 200| 55| 85| 65|[milk, 55]| [bread, 85]|[eggs, 65]|
| 300| 20| 125| 90|[milk, 20]|[bread, 125]|[eggs, 90]|
+--------+----------------+-----------------+----------------+----------+------------+----------+
+--------+----------------+-----------------+----------------+----------+------------+----------+--------------------+
|store_id|qty_on_hand_milk|qty_on_hand_bread|qty_on_hand_eggs| label1| label2| label3| labels|
+--------+----------------+-----------------+----------------+----------+------------+----------+--------------------+
| 100| 30| 105| 35|[milk, 30]|[bread, 105]|[eggs, 35]|[[milk, 30], [bre...|
| 200| 55| 85| 65|[milk, 55]| [bread, 85]|[eggs, 65]|[[milk, 55], [bre...|
| 300| 20| 125| 90|[milk, 20]|[bread, 125]|[eggs, 90]|[[milk, 20], [bre...|
+--------+----------------+-----------------+----------------+----------+------------+----------+--------------------+
+--------+----------------+-----------------+----------------+----------+------------+----------+------------+
|store_id|qty_on_hand_milk|qty_on_hand_bread|qty_on_hand_eggs| label1| label2| label3| labels|
+--------+----------------+-----------------+----------------+----------+------------+----------+------------+
| 100| 30| 105| 35|[milk, 30]|[bread, 105]|[eggs, 35]| [milk, 30]|
| 100| 30| 105| 35|[milk, 30]|[bread, 105]|[eggs, 35]|[bread, 105]|
| 100| 30| 105| 35|[milk, 30]|[bread, 105]|[eggs, 35]| [eggs, 35]|
| 200| 55| 85| 65|[milk, 55]| [bread, 85]|[eggs, 65]| [milk, 55]|
| 200| 55| 85| 65|[milk, 55]| [bread, 85]|[eggs, 65]| [bread, 85]|
| 200| 55| 85| 65|[milk, 55]| [bread, 85]|[eggs, 65]| [eggs, 65]|
| 300| 20| 125| 90|[milk, 20]|[bread, 125]|[eggs, 90]| [milk, 20]|
| 300| 20| 125| 90|[milk, 20]|[bread, 125]|[eggs, 90]|[bread, 125]|
| 300| 20| 125| 90|[milk, 20]|[bread, 125]|[eggs, 90]| [eggs, 90]|
+--------+----------------+-----------------+----------------+----------+------------+----------+------------+
+--------+----------------+-----------------+----------------+----------+------------+----------+------------+--------+-----------+
|store_id|qty_on_hand_milk|qty_on_hand_bread|qty_on_hand_eggs| label1| label2| label3| labels|CATEGORY|qty_on_hand|
+--------+----------------+-----------------+----------------+----------+------------+----------+------------+--------+-----------+
| 100| 30| 105| 35|[milk, 30]|[bread, 105]|[eggs, 35]| [milk, 30]| milk| 30|
| 100| 30| 105| 35|[milk, 30]|[bread, 105]|[eggs, 35]|[bread, 105]| bread| 105|
| 100| 30| 105| 35|[milk, 30]|[bread, 105]|[eggs, 35]| [eggs, 35]| eggs| 35|
| 200| 55| 85| 65|[milk, 55]| [bread, 85]|[eggs, 65]| [milk, 55]| milk| 55|
| 200| 55| 85| 65|[milk, 55]| [bread, 85]|[eggs, 65]| [bread, 85]| bread| 85|
| 200| 55| 85| 65|[milk, 55]| [bread, 85]|[eggs, 65]| [eggs, 65]| eggs| 65|
| 300| 20| 125| 90|[milk, 20]|[bread, 125]|[eggs, 90]| [milk, 20]| milk| 20|
| 300| 20| 125| 90|[milk, 20]|[bread, 125]|[eggs, 90]|[bread, 125]| bread| 125|
| 300| 20| 125| 90|[milk, 20]|[bread, 125]|[eggs, 90]| [eggs, 90]| eggs| 90|
+--------+----------------+-----------------+----------------+----------+------------+----------+------------+--------+-----------+
+--------+--------+-----------+
|store_id|CATEGORY|qty_on_hand|
+--------+--------+-----------+
| 100| milk| 30|
| 100| bread| 105|
| 100| eggs| 35|
| 200| milk| 55|
| 200| bread| 85|
| 200| eggs| 65|
| 300| milk| 20|
| 300| bread| 125|
| 300| eggs| 90|
+--------+--------+-----------+
Run Code Online (Sandbox Code Playgroud)
我们可以使用explode()函数来解决这个问题。在 Python 中,同样的事情可以用melt
# Loading the requisite packages
from pyspark.sql.functions import col, explode, array, struct, expr, sum, lit
# Creating the DataFrame
df = sqlContext.createDataFrame([(100,30,105,35),(200,55,85,65),(300,20,125,90)],('store_id','qty_on_hand_milk','qty_on_hand_bread','qty_on_hand_eggs'))
df.show()
+--------+----------------+-----------------+----------------+
|store_id|qty_on_hand_milk|qty_on_hand_bread|qty_on_hand_eggs|
+--------+----------------+-----------------+----------------+
| 100| 30| 105| 35|
| 200| 55| 85| 65|
| 300| 20| 125| 90|
+--------+----------------+-----------------+----------------+
Run Code Online (Sandbox Code Playgroud)
编写下面的函数,它应该是explode这个 DataFrame:
def to_explode(df, by):
# Filter dtypes and split into column names and type description
cols, dtypes = zip(*((c, t) for (c, t) in df.dtypes if c not in by))
# Spark SQL supports only homogeneous columns
assert len(set(dtypes)) == 1, "All columns have to be of the same type"
# Create and explode an array of (column_name, column_value) structs
kvs = explode(array([
struct(lit(c).alias("CATEGORY"), col(c).alias("qty_on_hand")) for c in cols
])).alias("kvs")
return df.select(by + [kvs]).select(by + ["kvs.CATEGORY", "kvs.qty_on_hand"])
Run Code Online (Sandbox Code Playgroud)
将这个 DataFrame 上的函数应用到explode它——
df = to_explode(df, ['store_id'])\
.drop('store_id')
df.show()
+-----------------+-----------+
| CATEGORY|qty_on_hand|
+-----------------+-----------+
| qty_on_hand_milk| 30|
|qty_on_hand_bread| 105|
| qty_on_hand_eggs| 35|
| qty_on_hand_milk| 55|
|qty_on_hand_bread| 85|
| qty_on_hand_eggs| 65|
| qty_on_hand_milk| 20|
|qty_on_hand_bread| 125|
| qty_on_hand_eggs| 90|
+-----------------+-----------+
Run Code Online (Sandbox Code Playgroud)
现在,我们需要qty_on_hand_从CATEGORY列中删除字符串。可以使用expr()函数来完成。注意expr子字符串的索引基于 1,而不是 0 -
df = df.withColumn('CATEGORY',expr('substring(CATEGORY, 13)'))
df.show()
+--------+-----------+
|CATEGORY|qty_on_hand|
+--------+-----------+
| milk| 30|
| bread| 105|
| eggs| 35|
| milk| 55|
| bread| 85|
| eggs| 65|
| milk| 20|
| bread| 125|
| eggs| 90|
+--------+-----------+
Run Code Online (Sandbox Code Playgroud)
最后,使用agg()函数聚合qty_on_hand分组的列-CATEGORY
df = df.groupBy(['CATEGORY']).agg(sum('qty_on_hand').alias('total_qty_on_hand'))
df.show()
+--------+-----------------+
|CATEGORY|total_qty_on_hand|
+--------+-----------------+
| eggs| 190|
| bread| 315|
| milk| 105|
+--------+-----------------+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8400 次 |
| 最近记录: |