PySpark DataFrame何时使用/不使用Select

问题描述 投票:0回答:1

基于PySpark文档:

DataFrame等效于Spark SQL中的关系表,可以使用SQLContext中的各种函数来创建

意思是我可以使用Select来显示列的值,但是,我有时有时会使用这两个等效代码:

# df is a sample DataFrame with column a
df.a
# or
df['a']

有时,当我使用select时,我可能会收到错误而不是错误,反之亦然,有时我必须使用Select。

例如,这是一个用于在给定图片问题中寻找狗的数据框:

joined_df.printSchema()
root
 |-- folder: string (nullable = true)
 |-- filename: string (nullable = true)
 |-- width: string (nullable = true)
 |-- height: string (nullable = true)
 |-- dog_list: array (nullable = true)
 |    |-- element: string (containsNull = true)

如果我想选择狗的详细信息并显示10行,此代码将显示错误:

print(joined_df.dog_list.show(truncate=False))

Traceback (most recent call last):
 File "<stdin>", line 2, in <module>
    print(joined_df.dog_list.show(truncate=False))
TypeError: 'Column' object is not callable

这不是:

print(joined_df.select('dog_list').show(truncate=False))

问题1:当我必须使用Select以及何时必须使用df.a或df [“ a”]

问题2:以上错误是什么意思? “列”对象不可调用

select pyspark-sql pyspark-dataframes
1个回答
1
投票
© www.soinside.com 2019 - 2024. All rights reserved.