You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Pandas 按条件删除行:estimated price减price大于1500时删除对应行

Pandas 按指定条件删除行实现方法

核心逻辑

你需要保留estimated price列减price列结果≤1500的行,反过来过滤掉差值>1500的行即可,以下是可直接运行的代码:

完整实现代码

import pandas as pd

# 1. 构造示例数据集(如果已经有数据集可跳过这一步)
df = pd.DataFrame(
    data=[[13295, 13795], [19990, 22275], [7295, 6498]],
    columns=['price', 'estimated price']
)

# 2. 过滤行:两种方案选其一即可
# 方案1:直接筛选保留符合要求的行,写法最简洁
df = df[df['estimated price'] - df['price'] <= 1500]

# 方案2:先找到要删除的行索引,再调用drop方法删除
# drop_idx = df[df['estimated price'] - df['price'] > 1500].index
# df.drop(drop_idx, inplace=True)

注意事项

因为你的列名estimated price带有空格,所以必须用df['列名']的格式访问列,不能用df.estimated price的点语法,否则会触发语法错误。
运行代码后你会得到仅剩索引0、2行的结果,符合预期要求。

内容的提问来源于stack exchange,提问作者Regnas01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 14:15:05