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

如何筛选pandas DataFrame中helpful_votes/total_votes≥50%的行

Pandas DataFrame按有用票占比过滤实现代码

你可以直接通过布尔索引完成筛选,注意需要额外处理total_votes为0的除零异常场景,避免代码报错:

基础实现(仅适用于total_votes全不为0的场景)

import pandas as pd

# 原始待处理DataFrame记为df
filtered_df = df[df['helpful_votes'] / df['total_votes'] >= 0.5]

兼容除零场景的优化实现

如果存在总票数为0的行,建议先过滤无效行再做筛选:

# 先排除总票数为0的行,再筛选有用票占比≥50%的记录
filtered_df = df[df['total_votes'] > 0].loc[lambda x: x['helpful_votes'] / x['total_votes'] >= 0.5]

待过滤的DataFrame示例如下:
待过滤的DataFrame示例

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 11:18:04