Python 如何在 PySpark 中仅打印 DataFrame 的某一列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35913506/
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 17:08:48  来源:igfitidea点击:

How to print only a certain column of DataFrame in PySpark?

pythonapache-sparkdataframepyspark

提问by mar tin

Can one use the actions collector taketo print only a given column of DataFrame?

可以使用这些操作collecttake仅打印 DataFrame 的给定列吗?

This

这个

df.col.collect()

gives error

给出错误

TypeError: 'Column' object is not callable

类型错误:“列”对象不可调用

and this:

和这个:

df[df.col].take(2)

gives

pyspark.sql.utils.AnalysisException: u"filter expression 'col' of type string is not a boolean.;"

pyspark.sql.utils.AnalysisException:u“字符串类型的过滤器表达式‘col’不是布尔值。;”

回答by zero323

selectand show:

selectshow

df.select("col").show()

or select, flatMap, collect:

select, flatMap, collect:

df.select("col").rdd.flatMap(list).collect()

Bracket notation (df[df.col]) is used only for logical slicing and columns by itself (df.col) are not distributed data structures but SQL expressions and cannot be collected.

方括号 ( df[df.col]) 仅用于逻辑切片,列本身 ( df.col) 不是分布式数据结构,而是 SQL 表达式,无法收集。