根据精确的字符串匹配过滤 Pandas 数据帧 [英] Filter Pandas Data Frame based on exact string match

查看:77
本文介绍了根据精确的字符串匹配过滤 Pandas 数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据字符串的精确匹配过滤熊猫数据框.

I want to filter a pandas data frame based on exact match of a string.

我有一个如下的数据框

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

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

I want to filter all the rows except the row that has 'aball'.As you can see I have one more entry with ids == 'aballl'. I want that filterd out. Hence the below code does not work:

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

即使 str.match 也不起作用

even str.match does not work

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

任何帮助将不胜感激.

推荐答案

保持简单,这应该可行:

Keeping it simple, this should work:

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

这篇关于根据精确的字符串匹配过滤 Pandas 数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆