我有一個資料框架,看起來像這樣的
我有一個資料框架。
--- ---- ------ ------- ------
| 身份證|fomrid|values|occ| comments
--- ---- ------ ------- ------
| 1| x1 | 22.0| 1| text1|
| 檢驗
| 1| x1 | 11 | 3| text3 |
| 1| x2 | 21 | 0 | text4 |
| 檢驗
--- ---- ------ ------- ------
我怎樣才能將其轉換為下面的資料框架?基本上,我想根據formId創建一個值和occ的串列。
--- ------ -------------- -------- ------
| Id|fomrid|List_values|List_occ| comments
--- ------ -------------- -------- ------
| 1| x1 |[22.0, test,11]|[1,2,3]| text1|
| 1| x2 | [21] | [0] | text4 |
| 2| p1 | [1] | [1] | text5 |
--- ----- --------------- ------- -------
uj5u.com熱心網友回復:
你可以使用collect_list來實作這一點。
使用spark sql
創建一個臨時視圖,并在你的spark會話中運行它
input_df.createOrReplaceTempView("my_temp_table_or_view")
output_df = sparkSession.sql("< insert sql below here>" )
SELECT
同上。
fomrid,
collect_list(values) as List_values,
collect_list(occ) as List_occ,
MIN(comments) as comments
FROM(評論)
my_temp_table_or_view
GROUPBY
Id, formrid
使用pyspark api
from pyspark.sql import functions as F
output_df = (
input_df.groupBy(["Id","fomrid"] )
.agg(
F.collect_list("values").alias("List_values") 。
F.collect_list("occ").alias("List_occ") 。
F.min("comments").alias("comments")
)
)
使用scala
val output_df = input_df.groupBy("Id", "fomrid")
.agg(
collect_list("values").alias("List_values")。
collect_list("occ").alias("List_occ")。
min("評論").alias("評論")
)
讓我知道這是否對你有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/310736.html
標籤:
