Fastest data storage method for looking up text in python

Viewed 75

I'm storing some details about notable people such as their names and dates of birth, along with a list of their quotes, structured into one long string

I'd like to be able to quickly search for a specific word or phrase, and return the name(s) of the people who have this word or phrase in their quotes. At the moment I am using a Pandas dataframe and it looks like this:

            name.value             dob.value                                             quotes
0       Jacinda Ardern  1980-07-26T00:00:00Z  ...for other women, it is totally unacceptable...
1          Helen Clark  1950-02-26T00:00:00Z  Girls can do anything. We do do anything and w...
2  Sonny Bill Williams  1985-08-03T00:00:00Z  I feel that if I am busting my arse, if I am s...
3        Peter Jackson  1961-10-31T00:00:00Z  Fantasy is an 'F' word that hopefully the five...
4  Katherine Mansfield  1888-10-14T00:00:00Z  Would you not like to try all sorts of lives —...

but I'm not sure this is the way to store this information for fastest access, or how to search efficiently for this.

2 Answers

Try this, it will return a list of the people who has the particular word in their quotes. And for searching efficiently I believe contains does a fairly good job.

def search_name(word):
    return df['name.value'].loc[df['quotes'].str.contains(word)].tolist()

This approach is similar to @Shivam's answer, but requires one fewer index command:

df.loc[df['quotes'].str.contains(word),'name.value']
Related