python - 基于精确字符串匹配过滤 Pandas Data Frame

标签 python string pandas

我想根据字符串的精确匹配来过滤 pandas 数据框。

我有如下数据框

df1 = pd.DataFrame({'vals': [1, 2, 3, 4,5], 'ids': [u'aball', u'bball', u'cnut', u'fball','aballl']})    

我想过滤除具有“aball”的行之外的所有行。如您所见,我还有一个 ids == 'aball' 的条目。我想过滤掉。因此下面的代码不起作用:

df1[df1['ids'].str.contains("aball")]    

连 str.match 都不工作

df1[df1['ids'].str.match("aball")] 

如有任何帮助,我们将不胜感激。

最佳答案

保持简单,这应该可行:

df1[df1['ids'] == "aball"] 

关于python - 基于精确字符串匹配过滤 Pandas Data Frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45164537/

相关文章:

unless 语句的 Python 语法

python - 如何在 Python 中从 yaml 转储中删除键/值对?

python - 快速半正弦逼近(Python/Pandas)

python - Pandas - 检查系列中的所有值是否都是 NaN

python - 查找一列的最小和最大行并减去另一列的值

python - 同时获取 `min`和 `idxmin`(或 `max`和 `idxmax`)("simultaneously")?

python - 使用Python的Requests模块登录ASP网站

python - 无法在 numpy 数组中插入三个连续的零

Python float 到字符串 : how to get '0.03' but '-.03'

java - 通过在垃圾收集之前使用反射来清理内容,安全地使用 String 作为密码