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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:30:19  来源:igfitidea点击:

Viewing the content of a Spark Dataframe Column

pythonapache-sparkdataframepyspark

提问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 dfcontains a column named 'zip_code'. So I can do df['zip_code']and it turns a pyspark.sql.dataframe.Columntype, 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 RDDand map over it

您可以访问底层RDD并对其进行映射

df.rdd.map(lambda r: r.zip_code).collect()

You can also use selectif you don't mind results wrapped using Rowobjects:

select如果您不介意使用Row对象包装的结果,您也可以使用:

df.select('zip_code').collect()

Finally, if you simply want to inspect content then showmethod should be enough:

最后,如果您只是想检查内容,那么show方法就足够了:

df.select('zip_code').show()

回答by Thomas Decaux

To view the complete content:

查看完整内容:

df.select("raw").take(1).foreach(println)

(showwill show you an overview).

show将向您展示概览)。