将3级嵌套字典键值转换为Pyspark DataFrame可以通过以下步骤完成:
from pyspark.sql import SparkSession
from pyspark.sql.functions import col
spark = SparkSession.builder.getOrCreate()
data = {
"key1": {
"subkey1": {"value1": 1, "value2": 2},
"subkey2": {"value1": 3, "value2": 4}
},
"key2": {
"subkey1": {"value1": 5, "value2": 6},
"subkey2": {"value1": 7, "value2": 8}
}
}
df = spark.createDataFrame([(k1, k2, v) for k1, v1 in data.items() for k2, v2 in v1.items() for k3, v in v2.items()])
df = df.toDF("key1", "key2", "value")
df.show()
这样,你就将3级嵌套字典键值成功转换为了Pyspark DataFrame。注意,这里没有提及任何腾讯云相关产品,因为这个问题并不涉及特定的云服务供应商。
领取专属 10元无门槛券
手把手带您无忧上云