gpt4 book ai didi

apache-spark - 访问 pyspark 数据框中的嵌套列

转载 作者:行者123 更新时间:2023-12-03 07:47:36 25 4
gpt4 key购买 nike

我有一个如下所示的 xml 文档:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Position>
<Search>
<Location>
<Region>OH</Region>
<Country>us</Country>
<Longitude>-816071</Longitude>
<Latitude>415051</Latitude>
</Location>
</Search>
</Position>

我将其读入数据框:

df = sqlContext.read.format('com.databricks.spark.xml').options(rowTag='Position').load('1.xml')

我可以看到 1 列:

df.columns
['Search']

print df.select("Search")
DataFrame[Search: struct<Location:struct<Country:string,Latitude:bigint,Longitude:bigint,Region:string>>]

如何访问嵌套列。例如位置.区域?

最佳答案

您可以执行如下操作:

df.select("Search.Location.*").show()

输出:

+-------+--------+---------+------+
|Country|Latitude|Longitude|Region|
+-------+--------+---------+------+
| us| 415051| -816071| OH|
+-------+--------+---------+------+

关于apache-spark - 访问 pyspark 数据框中的嵌套列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42240250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com