I'm trying to apply a TF-IDF in a Pandas column
data
all_cols
0 who is your hero and why
1 what do you do to relax
2 this is a hero
4 how many hours of sleep do you get a night
5 describe the last time you were relax
I know to use the CountVectorizer, I need to turn the column into list (and that's what I tried to do).
To apply TFIDF, I could not apply a list (and I tried to convert it to string).
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
import pandas as pd
df = pd.read_excel('data.xlsx')
col = df['all_cols']
corpus = col.values.tolist()
cv = CountVectorizer()
X = cv.fit_transform(corpus)
document = [' '.join(str(item)) for item in corpus]
tfidf_transformer=TfidfTransformer(smooth_idf=True,use_idf=True)
tfidf_transformer.fit(X)
feature_names=cv.get_feature_names()
tf_idf_vector=tfidf_transformer.transform(cv.transform([document]))
But I still have this error
AttributeError Traceback (most recent call last)
<ipython-input-239-92f296939ea7> in <module>()
16
---> 17 tf_idf_vector=tfidf_transformer.transform(cv.transform([documento]))
AttributeError: 'list' object has no attribute 'lower'