gpt4 book ai didi

python - 如何将字典列表转换为 Pyspark DataFrame

转载 作者:太空狗 更新时间:2023-10-30 00:37:44 25 4
gpt4 key购买 nike

我想将我的字典列表转换成 DataFrame。这是列表:

mylist = 
[
{"type_activity_id":1,"type_activity_name":"xxx"},
{"type_activity_id":2,"type_activity_name":"yyy"},
{"type_activity_id":3,"type_activity_name":"zzz"}
]

这是我的代码:

from pyspark.sql.types import StringType

df = spark.createDataFrame(mylist, StringType())

df.show(2,False)

+-----------------------------------------+
| value|
+-----------------------------------------+
|{type_activity_id=1,type_activity_id=xxx}|
|{type_activity_id=2,type_activity_id=yyy}|
|{type_activity_id=3,type_activity_id=zzz}|
+-----------------------------------------+

我假设我应该为每一列提供一些映射和类型,但我不知道该怎么做。

更新:

我也试过这个:

schema = ArrayType(
StructType([StructField("type_activity_id", IntegerType()),
StructField("type_activity_name", StringType())
]))
df = spark.createDataFrame(mylist, StringType())
df = df.withColumn("value", from_json(df.value, schema))

但随后我得到了 null 值:

+-----+
|value|
+-----+
| null|
| null|
+-----+

最佳答案

过去,您可以简单地将字典传递给 spark.createDataFrame(),但现在已弃用:

mylist = [
{"type_activity_id":1,"type_activity_name":"xxx"},
{"type_activity_id":2,"type_activity_name":"yyy"},
{"type_activity_id":3,"type_activity_name":"zzz"}
]
df = spark.createDataFrame(mylist)
#UserWarning: inferring schema from dict is deprecated,please use pyspark.sql.Row instead
# warnings.warn("inferring schema from dict is deprecated,"

正如这条警告消息所说,您应该改用 pyspark.sql.Row

from pyspark.sql import Row
spark.createDataFrame(Row(**x) for x in mylist).show(truncate=False)
#+----------------+------------------+
#|type_activity_id|type_activity_name|
#+----------------+------------------+
#|1 |xxx |
#|2 |yyy |
#|3 |zzz |
#+----------------+------------------+

这里我使用 ** ( keyword argument unpacking ) 将字典传递给 Row 构造函数。

关于python - 如何将字典列表转换为 Pyspark DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52238803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com