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
How to print only a certain column of DataFrame in PySpark?
提问by mar tin
Can one use the actions collect
or take
to print only a given column of DataFrame?
可以使用这些操作collect
或take
仅打印 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
select
and show
:
select
和show
:
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 表达式,无法收集。