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

基于多条件从DataFrame中移除行的技术实现需求问询

解决DataFrame多条件行移除问题

Hey there, let's sort out your DataFrame row filtering issue properly. You want to remove rows that fit any of these three criteria:

  • Text column has a string length of exactly 1
  • Associated_word column is null
  • Text column contains only numbers

输入数据

TextAssociated_word
CorperationCorp., Coperate
fflat,flt,flaat
100200,30,40
block
24fl24floor,25fl,36fl
centralcentre, cent
2030,40

期望输出

TextAssociated_word
CorperationCorp., Coperate
24fl24floor,25fl,36fl
centralcentre, cent

修正后的代码

# 组合三个过滤条件,保留不满足任一条件的行
df = df[
    ~(
        (df['Text'].str.len() == 1) |
        (df['Associated_word'].isnull()) |
        (df['Text'].str.isdigit())
    )
]

代码解释

Let's break down how this works:

  • df['Text'].str.len() == 1: Checks if the Text column's value has a string length of exactly 1
  • df['Associated_word'].isnull(): Flags rows where Associated_word is empty/null
  • df['Text'].str.isdigit(): Identifies rows where Text contains only numeric characters
  • The | operator means "OR" — so we're targeting rows that match any of the three conditions. The ~ symbol negates the entire result, so we keep only rows that don't meet any of the rules.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:24:42