Skip to content
Advertisement

How to search for name in dataframe

For example I want to find all the people that has “Abbott” in their name

0                        Abbing, Mr. Anthony
1                Abbott, Mr. Rossmore Edward
2           Abbott, Mrs. Stanton (Rosa Hunt)
3                        Abelson, Mr. Samuel
4      Abelson, Mrs. Samuel (Hannah Wizosky)
                       ...                  
886                  de Mulder, Mr. Theodore
887                de Pelsmaeker, Mr. Alfons
888                del Carlo, Mr. Sebastiano
889          van Billiard, Mr. Austin Blyler
890              van Melkebeke, Mr. Philemon
Name: Name, Length: 891, dtype: object

df.loc[name in df[“Name”]] I tried this and it didn’t work

'False: boolean label can not be used without a boolean index'

Advertisement

Answer

You can use str.contains with the column you are interested in searching

>>> import pandas as pd
>>> df = pd.DataFrame(data={'Name': ['Smith', 'Jones', 'Smithson']})
>>> df
       Name
0     Smith
1     Jones
2  Smithson
>>> df[df['Name'].str.contains('Smith')]
       Name
0     Smith
2  Smithson
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement