简体   繁体   English

查看 Spark 数据框列的内容

[英]Viewing the content of a Spark Dataframe Column

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.我正在尝试在 Python 中查看 Spark 数据框列的值。 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.使用 Spark 数据df.collect() ,我可以执行df.collect()来查看数据df.collect()的内容,但就我所见,没有针对 Spark 数据框列的最佳方法。

For example, the dataframe df contains a column named 'zip_code' .例如,数据'zip_code' df包含一个名为'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']并且它变成pyspark.sql.dataframe.Column类型,但我找不到查看df['zip_code']值的方法。

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:如果您不介意使用Row对象包装的结果,您也可以使用select

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()

To view the complete content:查看完整内容:

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

( show will show you an overview). show将向您展示概览)。

You can simply write:你可以简单地写:

df.select('your column's name').show()

In your case here, it will be:在您的情况下,它将是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM