我有一个PySpark数据框,其中包含列“results”。我希望在结果列中删除“Attributes”列。
数据框的模式(有更多列,但为了方便起见,我没有显示它们):
如何在PySpark中不使用udf完成此操作?
数据的一行:
数据框的模式(有更多列,但为了方便起见,我没有显示它们):
|-- results: struct (nullable = true)
| |-- l: array (nullable = true)
| | |-- element: struct (containsNull = true)
| | | |-- m: struct (nullable = true)
| | | | |-- Attributes: struct (nullable = true)
| | | | | |-- m: struct (nullable = true)
| | | | | | |-- Score: struct (nullable = true)
| | | | | | | |-- n: string (nullable = true)
| | | | |-- OtherInfo: struct (nullable = true)
| | | | | |-- l: array (nullable = true)
| | | | | | |-- element: struct (containsNull = true)
| | | | | | | |-- m: struct (nullable = true)
| | | | | | | | |-- Name: string (nullable = true)
如何在PySpark中不使用udf完成此操作?
数据的一行:
{
"results" : {
"l" : [
{
"m":{
"Attributes" : {
"m" : {
"Score" : {"n" : "85"}
}
},
"OtherInfo":{
"l" : [
{
"m" : {
"Name" : {"john"}
}
},
{
"m" : {
"Name" : "Cena"}
}
]
}
}
}
]
}
}
struct(x.m.OtherInfo as OtherInfo, x.m.OtherInfo2 as OtherInfo2)
。如果您有许多字段并且不想硬编码它们,可以从模式中获取字段名称,并使用Python循环构建转换表达式字符串。 - blackbishop