0

I am trying to search the following string in my pandas DataFrame but its not being able to find the string even though its present in the dataframe.

My code is:

df_temp = pd.DataFrame({'name' :['Wilkes, Mrs. James (Ellen Needs)']})
df_temp['name'].str.contains('Wilkes, Mrs. James (Ellen Needs)').sum()

This is giving me an output of zero where the expected output should be '1' . Is this because the string contains brackets or space or anything else? Is there a better method to search for strings in Pandas.

Any help would be really appreciated, Thankyou!

Jonathan DEKHTIAR
  • 3,456
  • 1
  • 21
  • 42
Nikhil Mishra
  • 1,182
  • 2
  • 18
  • 34
  • or else you may give: df_temp['name'].str.contains('Wilkes, Mrs. James (Ellen Needs)',regex=False).sum() which prints 1 – SudipM Jul 14 '18 at 12:54

1 Answers1

0

You should try doing this:

import pandas as pd

df = pd.DataFrame({'name': ['Wilkes, Mrs. James (Ellen Needs)']})
df_2 = df[df['name'].str.contains("Wilkes, Mrs. James \(Ellen Needs\)")]

print("Number of rows:", len(df_2.index))

Which returns:

Number of rows: 1
Jonathan DEKHTIAR
  • 3,456
  • 1
  • 21
  • 42