1
books_over10['Keywords'] = ""
    r = Rake() # Uses stopwords for english from NLTK, and all puntuation characters.
    for index, row in books_over10.iterrows():
        a=r.extract_keywords_from_text(row['bookTitle']) 
        c=r.get_ranked_phrases() # To get keyword phrases ranked with scores highest to lowest.
        books_over10.at[index, 'Keywords'] = c
    books_over10.head()

I am using the above code, in order to process all rows and extract keywords from each row from the column bookTitle and then insert them as a list into a new column named Keywords on the same row. The question is if there is a more efficient way to do this without iterating over all rows because it takes a lot of time. Any help would be appreciated. Thanks in advance !

Solution by Changming:

def extractor(row):
    a=r.extract_keywords_from_text(row)
    return r.get_ranked_phrases() # To get keyword phrases ranked with scores highest to lowest.

r = Rake() # Uses stopwords for english from NLTK, and all puntuation characters.
books_over10['Keywords'] = books_over10['bookTitle'].map(lambda row : extractor(row))

1 Answer 1

2

Try looking into map. Not sure exactly what Rake you are using, and the way you have it coded is a bit confusing, but the general syntax would be.

books_over10['Keywords'] = books_over10['bookTitle'].map(lambda a: FUNCTION(a))
2
  • Thank you that works! But do you know if it is better to use map or apply in this case? I've searched a bit and found that apply method could also work.
    – Ponx
    Jan 7, 2020 at 23:17
  • 1
    I typically find that map is faster; however, it should be easy to test with timeit, I would test but I don't have your code/data.
    – cmxu
    Jan 8, 2020 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.