我有一个 spark 数据框,这里是架构:
|-- eid: long (nullable = true)
|-- age: long (nullable = true)
|-- sex: long (nullable = true)
|-- father: array (nullable = true)
| |-- element: array (containsNull = true)
| | |-- element: long (containsNull = true)
和行样本:。
df.select(df['father']).show()
+--------------------+
| father|
+--------------------+
|[WrappedArray(-17...|
|[WrappedArray(-11...|
|[WrappedArray(13,...|
+--------------------+
类型是
DataFrame[father: array<array<bigint>>]
如何访问内部数组的每个元素?例如第一行的-17?
我尝试了不同的东西,比如 df.select(df['father'])(0)(0).show()
但没有成功。
最佳答案
如果我没记错的话,Python 中的语法是
df.select(df['father'])[0][0].show()
或
df.select(df['father']).getItem(0).getItem(0).show()
请在此处查看一些示例:http://spark.apache.org/docs/latest/api/python/pyspark.sql.html?highlight=column#pyspark.sql.Column
关于python - 访问 WrappedArray 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44468311/