python - 在 Dataframe 上使用 where() 或 filter() 时出错
问题描述
我想检查 Dataframe 列中的值first_id
是否在我拥有的 python id 列表中,如果是,那么它应该通过过滤器。
first_id_list = [1,2,3,4,5,6,7,8,9]
other_ids = id_dataframe.where(ids["first_id"] in first_id_list).select("other_id")
我正在用 python 编写,id_dataframe
是一个 PySpark Dataframe,first_id_list
是一个 python 整数列表。
我得到的错误是:
ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.
解决方案
这个表达式有问题:ids["first_id"] in first_id_list
ids["first_id"]
是一个 Pyspark 列。first_id_list
是一个 Python 列表。
where()
Pyspark Dataframe 方法需要一个布尔列来评估,但是你给它一个错误的 python 布尔表达式。
您必须使用 Pyspark Column 方法isin()
(文档:https ://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.Column.isin )
回答 :
other_ids = id_dataframe.where(ids["first_id"].isin(first_id_list)).select("other_id")
现在ids["first_id"].isin(first_id_list)
是一个返回布尔列的 DataFrame 布尔表达式。
推荐阅读
- html - 我正在尝试嵌入一个指向学习管理系统的链接,但 youtube 网址不起作用,它说移动设备只允许使用 html5 类型
- c - 虽然 GPIO2 上的 LED 工作正常,但在带有 LPC1768 的 GPIO1 上
- google-fit - 2021 年 4 月 27 日或之前更新 Google Fit 的开发者政策和 API
- c# - Microsoft Graph v1.0 getSchedule api 很慢
- c# - 在 OpenXml 中使用 FileStream 而不是 MemoryStream 创建新的 excel 电子表格文档
- ruby-on-rails - 如何使用 json 数据构建图像
- php - popen 执行一次后,php fread 返回空
- python - 我们可以为 python 创建一个 venv 并在 VSCode、Windows 10 的其他文件夹中使用该 venv 吗?
- firebase - 您使用什么文档 ID 将用户数据匿名保存到 Firebase?
- python - 使用美丽的汤选择 css 选择器