【问题标题】:Explode array values using PySpark使用 PySpark 分解数组值
【发布时间】:2020-06-29 17:44:21
【问题描述】:

我是 pyspark 的新手,我需要以一种将每个值分配给一个新列的方式分解我的值数组。我尝试使用爆炸,但无法获得所需的输出。以下是我的输出

+---------------+----------+------------------+----------+---------+------------+--------------------+
|account_balance|account_id|credit_Card_Number|first_name|last_name|phone_number|        transactions|
+---------------+----------+------------------+----------+---------+------------+--------------------+
|         100000|     12345|             12345|       abc|      xyz|  1234567890|[1000, 01/06/2020...|
|         100000|     12345|             12345|       abc|      xyz|  1234567890|[1100, 02/06/2020...|
|         100000|     12345|             12345|       abc|      xyz|  1234567890|[6146, 02/06/2020...|
|         100000|     12345|             12345|       abc|      xyz|  1234567890|[253, 03/06/2020,...|
|         100000|     12345|             12345|       abc|      xyz|  1234567890|[4521, 04/06/2020...|
|         100000|     12345|             12345|       abc|      xyz|  1234567890|[955, 05/06/2020,...|
+---------------+----------+------------------+----------+---------+------------+--------------------+

下面是程序的架构

root
 |-- account_balance: long (nullable = true)
 |-- account_id: long (nullable = true)
 |-- credit_Card_Number: long (nullable = true)
 |-- first_name: string (nullable = true)
 |-- last_name: string (nullable = true)
 |-- phone_number: long (nullable = true)
 |-- transactions: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- amount: long (nullable = true)
 |    |    |-- date: string (nullable = true)
 |    |    |-- shop: string (nullable = true)
 |    |    |-- transaction_code: string (nullable = true)

我想要一个输出,其中我有额外的数量、日期、商店、交易代码列以及它们各自的值

amount date        shop     transaction_code
1000   01/06/2020  amazon      buy
1100   02/06/2020  amazon      sell
6146   02/06/2020  ebay        buy
253    03/06/2020  ebay        buy 
4521   04/06/2020  amazon      buy
955    05/06/2020  amazon      buy

【问题讨论】:

    标签: apache-spark hadoop pyspark apache-spark-sql pyspark-dataframes


    【解决方案1】:

    使用explode,然后拆分struct文件,最后丢弃新分解的和transactions数组列。

    Example:

    from pyspark.sql.functions import *
    
    #got only some columns from json
    df.printSchema()
    #root
    # |-- account_balance: long (nullable = true)
    # |-- transactions: array (nullable = true)
    # |    |-- element: struct (containsNull = true)
    # |    |    |-- amount: long (nullable = true)
    # |    |    |-- date: string (nullable = true)
    df.selectExpr("*","explode(transactions)").select("*","col.*").drop(*['col','transactions']).show()
    #+---------------+------+--------+
    #|account_balance|amount|    date|
    #+---------------+------+--------+
    #|             10|  1000|20200202|
    #+---------------+------+--------+
    

    【讨论】:

    • 很好的解决方案!非常感谢!
    猜你喜欢
    • 2021-01-12
    • 2018-03-02
    • 2021-03-27
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多