我正在嘗試合并資料集中的資料,如下所示:
| ID | 短信 | 詳細描述 | 其他領域 |
|---|---|---|---|
| 123 | 內容短信 | 內容描述 | xxx |
| 123 | 內容短信2 | 內容描述2 | xxx |
| 123 | 內容短信3 | 內容描述3 | xxx |
| 456 | 內容短信4 | 內容描述 | xxx |
sms 和 longDescription 具有以下結構:
sms:array
|----element:struct
|----content:string
|----languageId:string
目的是捕獲具有相同 Id 的資料并將列合并sms到longDescription一個具有多個結構的陣列中(使用languageIDas 鍵):
| ID | 短信 | 詳細描述 | 其他領域 |
|---|---|---|---|
| 123 | 內容短信,內容短信2,內容短信3 | 內容描述,內容描述2,內容描述3 | xxx |
| 456 | 內容短信4 | 內容描述 | xxx |
我試過使用
x = df.select("*").groupBy("id").agg( collect_list("sms"))
但結果是:
collect_list(longDescription): array (nullable = false)
| |-- element: array (containsNull = false)
| | |-- element: struct (containsNull = true)
| | | |-- content: string (nullable = true)
| | | |-- languageId: string (nullable = true)
這是一個太多的陣列,因為目標是擁有一個結構陣列以獲得以下結果:
sms: [{content: 'aze', languageId:'en-GB'},{content: 'rty', languageId:'fr-BE'},{content: 'poiu', languageId:'nl-BE'}]
uj5u.com熱心網友回復:
您正在尋找flatten功能:
x = df.groupBy("id").agg(flatten(collect_list("sms")))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/417914.html
標籤:
下一篇:如何按ID對物件陣列進行排序?
