【问题标题】:Convert PySpark dataframe column type to string and replace the square brackets将 PySpark 数据框列类型转换为字符串并替换方括号
【发布时间】:2017-05-02 05:36:39
【问题描述】:

我需要将 PySpark df 列类型从数组转换为字符串,并删除方括号。这是数据框的架构。需要处理的列是 CurrencyCode 和 TicketAmount

>>> plan_queryDF.printSchema()
root
 |-- event_type: string (nullable = true)
 |-- publishedDate: string (nullable = true)
 |-- plannedCustomerChoiceID: string (nullable = true)
 |-- assortedCustomerChoiceID: string (nullable = true)
 |-- CurrencyCode: array (nullable = true)
 |    |-- element: string (containsNull = true)
 |-- TicketAmount: array (nullable = true)
 |    |-- element: string (containsNull = true)
 |-- currentPlan: boolean (nullable = true)
 |-- originalPlan: boolean (nullable = true)
 |-- globalId: string (nullable = true)
 |-- PlanJsonData: string (nullable = true)

数据帧中的样本数据

+--------------------+--------------------+-----------------------+------------------------+------------+------------+-----------+------------+------------+--------------------+
|          event_type|       publishedDate|plannedCustomerChoiceID|assortedCustomerChoiceID|CurrencyCode|TicketAmount|currentPlan|originalPlan|    globalId|        PlanJsonData|
+--------------------+--------------------+-----------------------+------------------------+------------+------------+-----------+------------+------------+--------------------+
|PlannedCustomerCh...|2016-08-23T04:46:...|   087d1ff1-5f3a-496...|    2539cc4a-37e5-4f3...|       [GBP]|         [0]|      false|       false|000576015000|{"httpStatus":200...|
|PlannedCustomerCh...|2016-08-23T04:30:...|   0a1af217-d1e8-4ab...|    61bc5fda-0160-484...|       [CNY]|       [329]|       true|       false|000189668017|{"httpStatus":200...|
|PlannedCustomerCh...|2016-08-23T05:49:...|   1028b477-f93e-47f...|    c6d5b761-94f2-454...|       [JPY]|      [3400]|       true|       false|000576058003|{"httpStatus":200...|

我该怎么做?目前我正在对字符串进行强制转换,然后用 regexp_replace 替换方括号。但是当我处理大量数据时,这种方法会失败。

还有其他方法可以吗?

这就是我想要的。

+--------------------+--------------------+-----------------------+------------------------+------------+------------+-----------+------------+------------+--------------------+
|          event_type|       publishedDate|plannedCustomerChoiceID|assortedCustomerChoiceID|CurrencyCode|TicketAmount|currentPlan|originalPlan|    globalId|        PlanJsonData|
+--------------------+--------------------+-----------------------+------------------------+------------+------------+-----------+------------+------------+--------------------+
|PlannedCustomerCh...|2016-08-23T04:46:...|   087d1ff1-5f3a-496...|    2539cc4a-37e5-4f3...|       GBP|         0|      false|       false|000576015000|{"httpStatus":200...|
|PlannedCustomerCh...|2016-08-23T04:30:...|   0a1af217-d1e8-4ab...|    61bc5fda-0160-484...|       CNY|       329|       true|       false|000189668017|{"httpStatus":200...|
|PlannedCustomerCh...|2016-08-23T05:49:...|   1028b477-f93e-47f...|    c6d5b761-94f2-454...|       JPY|      3400|       true|       false|000576058003|{"httpStatus":200...|

【问题讨论】:

  • 你的 spark 版本是什么?你可以试试collect_list("TicketAmount")[0], collect_list("CurrencyCode")[0]
  • 运行版本 1.6.1
  • collect_list("TicketAmount")[0] 不起作用。 AttributeError: 'DataFrame' 对象没有属性 'collect_list'
  • plan_queryDF.select(" event_type, publishedDate, plannedCustomerChoiceID, assortedCustomerChoiceID, collect_list("CurrencyCode")[0], collect_list("TicketAmount")[0], currentPlan, originalPlan, globalId, PlanJsonData ")
  • 我有一个workaroud,在查询父数据帧时我做了一个转换为字符串,然后通过一个udf运行数据帧。

标签: python pyspark apache-spark-sql


【解决方案1】:

你可以试试getItem(0):

df \
    .withColumn("CurrencyCode", df["CurrencyCode"].getItem(0).cast("string")) \
    .withColumn("TicketAmount", df["TicketAmount"].getItem(0).cast("string")) 

最终转换为字符串是可选的。

【讨论】:

    猜你喜欢
    • 2017-12-19
    • 2020-07-25
    • 2017-03-22
    • 1970-01-01
    • 2021-12-01
    • 2016-08-30
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多