[986] Filter rows by containing a particular word in Pandas
In Pandas, you can filter rows based on whether a specific column contains a particular word or substring. Here are a few ways to achieve this:
-
Using
str.contains()
:- To filter rows where a specific column (let’s say ‘ids’) contains the word “ball,” you can use the following code:
Python
filtered_df = df[df['ids'].str.contains("ball")] - This will return rows where the ‘ids’ column contains the substring “ball” 1.
- To filter rows where a specific column (let’s say ‘ids’) contains the word “ball,” you can use the following code:
-
Inverting the Filter:
- If you want to find rows that do not contain the string “ball,” you can use the negation operator (
~
):Pythoninverted_df = df[~df['ids'].str.contains("ball")] - The
~
negates the condition, resulting in rows where the ‘ids’ column does not contain “ball” 1.
- If you want to find rows that do not contain the string “ball,” you can use the negation operator (
-
Partial Match and Remaining String:
分类:
Python Study
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2023-04-23 【834】GPT相关
2023-04-23 【833】BERT相关(Hugging Face)
2021-04-23 【553】rtree,R树 相关
2018-04-23 【312】◀▶ arcpy 常用函数说明