基于多条件从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
输入数据
| Text | Associated_word |
|---|---|
| Corperation | Corp., Coperate |
| f | flat,flt,flaat |
| 100 | 200,30,40 |
| block | |
| 24fl | 24floor,25fl,36fl |
| central | centre, cent |
| 20 | 30,40 |
期望输出
| Text | Associated_word |
|---|---|
| Corperation | Corp., Coperate |
| 24fl | 24floor,25fl,36fl |
| central | centre, 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 1df['Associated_word'].isnull(): Flags rows where Associated_word is empty/nulldf['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
相关产品推荐
相关产品推荐

