[英]How to split list of dictionary in one column into two columns in pyspark dataframe?
[英]How to split a list of objects to separate columns in pyspark dataframe
我在 dataframe 中有一個列作為對象列表(結構數組),例如
column: [{key1:value1}, {key2:value2}, {key3:value3}]
我想將此列拆分為單獨的列,其中鍵名作為列名,值作為同一行中的列值。
最終結果如
key1:value1, key2:value2, key3:value3
如何在 pyspark 中實現這一點?
例如
創建 dataframe 的示例數據:
my_new_schema = StructType([
StructField('id', LongType()),
StructField('countries', ArrayType(StructType([
StructField('name', StringType()),
StructField('capital', StringType())
])))
])
l = [(1, [
{'name': 'Italy', 'capital': 'Rome'},
{'name': 'Spain', 'capital': 'Madrid'}
])
]
dz = spark.createDataFrame(l, schema=my_new_schema)
# we have array of structs:
dz.show(truncate=False)
+---+--------------------------------+
|id |countries |
+---+--------------------------------+
|1 |[{Italy, Rome}, {Spain, Madrid}]|
+---+--------------------------------+
預期 output:
+---+--------+---------+
|id |Italy | Spain |
+---+------------------+
|1 |Rome | Madrid |
+---+--------+---------+
inline
countries
數組,然后 pivot 國家name
列:
import pyspark.sql.functions as F
dz1 = dz.selectExpr(
"id",
"inline(countries)"
).groupBy("id").pivot("name").agg(
F.first("capital")
)
dz1.show()
#+---+-----+------+
#|id |Italy|Spain |
#+---+-----+------+
#|1 |Rome |Madrid|
#+---+-----+------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.