从包含嵌套值的Spark列中提取值[duplicate]

问题描述 投票:2回答:1

这个问题在这里已有答案:

这是我的mongodb集合的模式的一部分:

|-- variables: struct (nullable = true)  
|    |-- actives: struct (nullable = true)  
|    |    |-- data: struct (nullable = true)  
|    |    |    |-- 0: struct (nullable = true)  
|    |    |    |    |--active: integer (nullable = true)  
|    |    |    |    |-- inactive: integer (nullable = true)

我已经获取了该集合并将其存储在Spark数据帧中,现在我正在尝试提取变量列中的最内层值。

df_temp = df1.select(df1.variables.actives.data)

这完全正常,我能够获得数据结构的内部结构。

+----------------------+  
|variables.actives.data|  
+----------------------+  
|  [[1,32,0.516165...|  
|  [[1,30,1.173139...|  
|  [[4,18,0.160088...|

但是,一旦我尝试进一步:

df_temp = df1.select(df1.variables.actives.data.0.active)

我收到无效的语法错误。

df_temp = df1.select(df1.variables.actives.data.0.active) ^ SyntaxError:语法无效

问题是我的内部字段的密钥名称是一个数字,我找不到内部字段键名称是数字的示例。

实现从数据帧中检索最内层值(活动和非活动)的目标的最佳方法是什么?

apache-spark pyspark spark-dataframe
1个回答
1
投票

你可以试试:

df_temp = df1.select(df1.variables.actives.data["0"].active)
© www.soinside.com 2019 - 2024. All rights reserved.