How to get tfidf with pandas dataframe?

后端 未结 3 1251
天涯浪人
天涯浪人 2020-12-05 04:22

I want to calculate tf-idf from the documents below. I\'m using python and pandas.

import pandas as pd
df = pd.DataFrame({\'docId\': [1,2,3], 
                       


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 05:05

    A simple solution is to use texthero:

    import texthero as hero
    df['tfidf'] = hero.tfidf(df['sent'])
    
    In [5]: df.head()
    Out[5]:
       docId                         sent                                              tfidf
    0      1   This is the first sentence  [0.3816141458138271, 0.6461289150464732, 0.381...
    1      2  This is the second sentence  [0.3816141458138271, 0.0, 0.3816141458138271, ...
    2      3   This is the third sentence  [0.3816141458138271, 0.0, 0.3816141458138271, ...
    

提交回复
热议问题