Python 查看 Spark 数据框列的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31124131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Viewing the content of a Spark Dataframe Column
提问by John Lin
I'm using Spark 1.3.1.
我正在使用 Spark 1.3.1。
I am trying to view the values of a Spark dataframe column in Python. With a Spark dataframe, I can do df.collect()
to view the contents of the dataframe, but there is no such method for a Spark dataframe column as best as I can see.
我正在尝试在 Python 中查看 Spark 数据框列的值。使用 Spark 数据框,我可以df.collect()
查看数据框的内容,但就我所见,没有针对 Spark 数据框列的最佳方法。
For example, the dataframe df
contains a column named 'zip_code'
. So I can do df['zip_code']
and it turns a pyspark.sql.dataframe.Column
type, but I can't find a way to view the values in df['zip_code']
.
例如,数据框df
包含一个名为 的列'zip_code'
。所以我可以做df['zip_code']
,它会变成一种pyspark.sql.dataframe.Column
类型,但我找不到一种方法来查看df['zip_code']
.
采纳答案by zero323
You can access underlying RDD
and map over it
您可以访问底层RDD
并对其进行映射
df.rdd.map(lambda r: r.zip_code).collect()
You can also use select
if you don't mind results wrapped using Row
objects:
select
如果您不介意使用Row
对象包装的结果,您也可以使用:
df.select('zip_code').collect()
Finally, if you simply want to inspect content then show
method should be enough:
最后,如果您只是想检查内容,那么show
方法就足够了:
df.select('zip_code').show()
回答by Thomas Decaux
To view the complete content:
查看完整内容:
df.select("raw").take(1).foreach(println)
(show
will show you an overview).
(show
将向您展示概览)。