Chris90
Chris90

Reputation: 1998

Filtering for a column for all rows that have a specific string in it

I am trying to get all unique values in a column that have a specific word in it,

for example if I want to find all values that have the word bird in it, regardless of Capitalization of first letter or not, how can I filter for that?

df.animal.unique() would give me all unique values in the column but I want only the ones that have Bird in the string or name if that makes sense, Thanks!

DF

 | Animal |
Eagle Bird
PigeonBird
Whale Fish
Dolphinfish
peacockbird
hawkBird
Falc Bird

Upvotes: 0

Views: 49

Answers (2)

jezrael
jezrael

Reputation: 862541

Because need:

I want to find all values that have the word bird in it, regardless of Capitalization of first letter or not

use Series.str.contains with both words:

df = df[df.Animal.str.contains('bird|Bird')]

Or:

df = df[df.Animal.str.contains('[bB]ird')]

print (df)
        Animal
0   Eagle Bird
1   PigeonBird
4  peacockbird
5     hawkBird
6    Falc Bird

Upvotes: 1

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10960

Try using str.contains

df[df.animal.str.contains('bird', case=False)]

Upvotes: 1

Related Questions