在Pandas中使用TF-IDF提取文本特征可以通过以下步骤实现:
from sklearn.feature_extraction.text import TfidfVectorizer
import pandas as pd
data = {'text': ['This is a sample text for TF-IDF example',
'TF-IDF is a technique used in text mining',
'It calculates the importance of each word in a document']}
df = pd.DataFrame(data)
tfidf = TfidfVectorizer()
tfidf_matrix = tfidf.fit_transform(df['text'])
tfidf_df = pd.DataFrame(tfidf_matrix.toarray(), columns=tfidf.get_feature_names_out())
现在,tfidf_df中包含了每个文档中每个单词的TF-IDF值作为特征。您可以将这些特征用于机器学习模型中进行文本分类、聚类等任务。