Filter Pandas Data Frame based on exact string match

Viewed 20423

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

I have a data frame as below

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

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")]    

even str.match does not work

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

Any help would be greatly appreciated.

2 Answers
Related