我正在尝试解析JSON并使用Python Spark向数据帧添加列:
tableDF = spark.sql("select * from transaction")
stats_df = parseJSONCols(tableDF)
def parseJSONCols(df):
res = df
cols = ['State']
for i in cols:
schema = spark.read
.json(res.rdd.map(lambda x: x[i]))
.schema
res = res.withColumn("selectedState", lit(filterSelectedState(col(i))))
return res
其中State
是具有以下结构的JSON字符串:
[
{
isSelected: true,
name: 'x'
},
{
isSelected: false,
name: 'y'
}
]
我想提取isSelected
字段的值,并将其添加到tableDF
的selected
列中。你知道吗
有人能帮我做到这一点吗?你知道吗
你可能想要这样的东西:
但是,查看say
stats_df.take(2)
的输出会很有用,这样我们就可以看到您正在处理的DF的确切结构。你知道吗相关问题 更多 >
编程相关推荐