Skip to content Skip to sidebar Skip to footer

Apply Nltk Rake To Each Row In Dataframe

I'd like to apply the Rake function (https://pypi.org/project/rake-nltk/) to each row in my dataframe. I can apply the function individually to a specific row, but not append it t

Solution 1:

r.extract_keywords_from_text(x) will return you None

import pandas as pd
from  rake_nltk import Rake  

r = Rake()    

df=pd.DataFrame(data = ['machine learning and fraud detection are a must learn',
                  'monte carlo method is great and so is hmm,pca, svm and neural net',
                  'clustering and cloud',
                  'logistical regression and data management and fraud detection'] ,columns = ['Comments'])


 def rake_implement(x,r):
     r.extract_keywords_from_text(x)
     return r.get_ranked_phrases()

df['new_col'] =df['Comments'].apply(lambda x: rake_implement(x,r))
print(df['new_col'])
#o/p
0      [must learn, machine learning, fraud detection]
1    [monte carlo method, neural net, svm, pca, hmm...
2                                  [clustering, cloud]
3    [logistical regression, fraud detection, data ...
Name: new_col, dtype: object  

Post a Comment for "Apply Nltk Rake To Each Row In Dataframe"