gpt4 book ai didi

python - 如何通过 bool 列过滤 Spark 数据帧?

转载 作者:太空狗 更新时间:2023-10-29 22:18:24 25 4
gpt4 key购买 nike

我创建了一个具有以下架构的数据框:

In [43]: yelp_df.printSchema()
root
|-- business_id: string (nullable = true)
|-- cool: integer (nullable = true)
|-- date: string (nullable = true)
|-- funny: integer (nullable = true)
|-- id: string (nullable = true)
|-- stars: integer (nullable = true)
|-- text: string (nullable = true)
|-- type: string (nullable = true)
|-- useful: integer (nullable = true)
|-- user_id: string (nullable = true)
|-- name: string (nullable = true)
|-- full_address: string (nullable = true)
|-- latitude: double (nullable = true)
|-- longitude: double (nullable = true)
|-- neighborhoods: string (nullable = true)
|-- open: boolean (nullable = true)
|-- review_count: integer (nullable = true)
|-- state: string (nullable = true)

我只想选择“打开”列为“真”的记录。我在 PySpark 中运行的以下命令不返回任何内容:

yelp_df.filter(yelp_df["open"] == "true").collect()

最佳答案

您比较的数据类型不正确。 open被列为 bool 值,而不是字符串,所以做 yelp_df["open"] == "true"不正确 - "true"是一个字符串。

相反你想做

yelp_df.filter(yelp_df["open"] == True).collect()

这正确地比较了 open 的值反对 bool 原语 True , 而不是非 bool 字符串 "true" .

关于python - 如何通过 bool 列过滤 Spark 数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36784000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com