Python 使用lambda函数对整个列进行柠檬化
我对这段代码进行了一个句子测试,我想对它进行转换,这样我就可以对整个列进行柠檬化,其中每一行都由没有标点符号的单词组成,比如:lakarivas calcetin hombres lakarivas shoesPython 使用lambda函数对整个列进行柠檬化,python,lambda,nltk,wordnet,lemmatization,Python,Lambda,Nltk,Wordnet,Lemmatization,我对这段代码进行了一个句子测试,我想对它进行转换,这样我就可以对整个列进行柠檬化,其中每一行都由没有标点符号的单词组成,比如:lakarivas calcetin hombres lakarivas shoes import wordnet, nltk nltk.download('wordnet') from nltk.stem import WordNetLemmatizer from nltk.corpus import wordnet import pandas as pd d
import wordnet, nltk
nltk.download('wordnet')
from nltk.stem import WordNetLemmatizer
from nltk.corpus import wordnet
import pandas as pd
df = pd.read_excel(r'C:\Test2\test.xlsx')
# Init the Wordnet Lemmatizer
lemmatizer = WordNetLemmatizer()
sentence = 'FINAL_KEYWORDS'
def get_wordnet_pos(word):
"""Map POS tag to first character lemmatize() accepts"""
tag = nltk.pos_tag([word])[0][1][0].upper()
tag_dict = {"J": wordnet.ADJ,
"N": wordnet.NOUN,
"V": wordnet.VERB,
"R": wordnet.ADV}
return tag_dict.get(tag, wordnet.NOUN)
#Lemmatize a Sentence with the appropriate POS tag
sentence = "The striped bats are hanging on their feet for best"
print([lemmatizer.lemmatize(w, get_wordnet_pos(w)) for w in nltk.word_tokenize(sentence)])
假设列名是df['keywords'],你能帮助我使用lambda函数来对整个列进行线性化,就像我对上面的句子进行线性化一样吗
提前向您表示感谢给您:
apply
应用于该列的句子语句
作为输入,并以类似于在print语句中使用的方式应用您编写的函数# Lemmatize a Sentence with the appropriate POS tag
df['keywords'] = df['keywords'].apply(lambda sentence: [lemmatizer.lemmatize(w, get_wordnet_pos(w)) for w in nltk.word_tokenize(sentence)])
作为引理化句子(关键字使用“”):
给你:
apply
应用于该列的句子语句
作为输入,并以类似于在print语句中使用的方式应用您编写的函数# Lemmatize a Sentence with the appropriate POS tag
df['keywords'] = df['keywords'].apply(lambda sentence: [lemmatizer.lemmatize(w, get_wordnet_pos(w)) for w in nltk.word_tokenize(sentence)])
作为引理化句子(关键字使用“”):
这个问题需要更多的注意;)(这个问题需要更多的注意;)磅