首页 文章

在PySpark Dataframe中重命名透视和聚合列

提问于
浏览
3

使用如下数据框:

from pyspark.sql.functions import avg, first

rdd = sc.parallelize(
    [
        (0, "A", 223,"201603", "PORT"), 
        (0, "A", 22,"201602", "PORT"), 
        (0, "A", 422,"201601", "DOCK"), 
        (1,"B", 3213,"201602", "DOCK"), 
        (1,"B", 3213,"201601", "PORT"), 
        (2,"C", 2321,"201601", "DOCK")
    ]
)
df_data = sqlContext.createDataFrame(rdd, ["id","type", "cost", "date", "ship"])

df_data.show()

我做了一个支点,

df_data.groupby(df_data.id, df_data.type).pivot("date").agg(avg("cost"), first("ship")).show()

+---+----+----------------+--------------------+----------------+--------------------+----------------+--------------------+
| id|type|201601_avg(cost)|201601_first(ship)()|201602_avg(cost)|201602_first(ship)()|201603_avg(cost)|201603_first(ship)()|
+---+----+----------------+--------------------+----------------+--------------------+----------------+--------------------+
|  2|   C|          2321.0|                DOCK|            null|                null|            null|                null|
|  0|   A|           422.0|                DOCK|            22.0|                PORT|           223.0|                PORT|
|  1|   B|          3213.0|                PORT|          3213.0|                DOCK|            null|                null|
+---+----+----------------+--------------------+----------------+--------------------+----------------+--------------------+

但是我为列提供了这些非常复杂的名称 . 在聚合上应用 alias 通常有效,但由于 pivot 在这种情况下名称更糟:

+---+----+--------------------------------------------------------------+------------------------------------------------------------------+--------------------------------------------------------------+------------------------------------------------------------------+--------------------------------------------------------------+------------------------------------------------------------------+
| id|type|201601_(avg(cost),mode=Complete,isDistinct=false) AS cost#1619|201601_(first(ship)(),mode=Complete,isDistinct=false) AS ship#1620|201602_(avg(cost),mode=Complete,isDistinct=false) AS cost#1619|201602_(first(ship)(),mode=Complete,isDistinct=false) AS ship#1620|201603_(avg(cost),mode=Complete,isDistinct=false) AS cost#1619|201603_(first(ship)(),mode=Complete,isDistinct=false) AS ship#1620|
+---+----+--------------------------------------------------------------+------------------------------------------------------------------+--------------------------------------------------------------+------------------------------------------------------------------+--------------------------------------------------------------+------------------------------------------------------------------+
|  2|   C|                                                        2321.0|                                                              DOCK|                                                          null|                                                              null|                                                          null|                                                              null|
|  0|   A|                                                         422.0|                                                              DOCK|                                                          22.0|                                                              PORT|                                                         223.0|                                                              PORT|
|  1|   B|                                                        3213.0|                                                              PORT|                                                        3213.0|                                                              DOCK|                                                          null|                                                              null|
+---+----+--------------------------------------------------------------+------------------------------------------------------------------+--------------------------------------------------------------+------------------------------------------------------------------+--------------------------------------------------------------+------------------------------------------------------------------+

有没有办法在数据透视和聚合上动态重命名列名?

3 回答

  • 4

    一个简单的正则表达式可以做到这一点:

    import re
    
    def clean_names(df):
        p = re.compile("^(\w+?)_([a-z]+)\((\w+)\)(?:\(\))?")
        return df.toDF(*[p.sub(r"\1_\3", c) for c in df.columns])
    
    pivoted = df_data.groupby(...).pivot(...).agg(...)
    
    clean_names(pivoted).printSchema()
    ## root
    ##  |-- id: long (nullable = true)
    ##  |-- type: string (nullable = true)
    ##  |-- 201601_cost: double (nullable = true)
    ##  |-- 201601_ship: string (nullable = true)
    ##  |-- 201602_cost: double (nullable = true)
    ##  |-- 201602_ship: string (nullable = true)
    ##  |-- 201603_cost: double (nullable = true)
    ##  |-- 201603_ship: string (nullable = true)
    

    如果要保留函数名称,请将替换模式更改为例如 \1_\2_\3 .

  • 0

    一种简单的方法是在聚合函数之后使用别名 . 我从你创建的df_data spark dataFrame开始 .

    df_data.groupby(df_data.id, df_data.type).pivot("date").agg(avg("cost").alias("avg_cost"), first("ship").alias("first_ship")).show()
    +---+----+---------------+-----------------+---------------+-----------------+---------------+-----------------+
    | id|type|201601_avg_cost|201601_first_ship|201602_avg_cost|201602_first_ship|201603_avg_cost|201603_first_ship|
    +---+----+---------------+-----------------+---------------+-----------------+---------------+-----------------+
    |  1|   B|         3213.0|             PORT|         3213.0|             DOCK|           null|             null|
    |  2|   C|         2321.0|             DOCK|           null|             null|           null|             null|
    |  0|   A|          422.0|             DOCK|           22.0|             PORT|          223.0|             PORT|
    +---+----+---------------+-----------------+---------------+-----------------+---------------+-----------------+
    

    列名称将是“original_column_name_aliased_column_name”的形式 . 对于您的情况,original_column_name将为201601,aliased_column_name将为avg_cost,列名称为201601_avg_cost(由下划线“_”链接) .

  • 4

    您可以直接为聚合设置别名:

    pivoted = df_data \
        .groupby(df_data.id, df_data.type) \
        .pivot("date") \
        .agg(
           avg('cost').alias('cost'),
           first("ship").alias('ship')
        )
    
    pivoted.printSchema()
    ##root
    ##|-- id: long (nullable = true)
    ##|-- type: string (nullable = true)
    ##|-- 201601_cost: double (nullable = true)
    ##|-- 201601_ship: string (nullable = true)
    ##|-- 201602_cost: double (nullable = true)
    ##|-- 201602_ship: string (nullable = true)
    ##|-- 201603_cost: double (nullable = true)
    ##|-- 201603_ship: string (nullable = true)
    

相关问题