Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用lambda函数对整个列进行柠檬化_Python_Lambda_Nltk_Wordnet_Lemmatization - Fatal编程技术网

Python 使用lambda函数对整个列进行柠檬化

Python 使用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

我对这段代码进行了一个句子测试,我想对它进行转换,这样我就可以对整个列进行柠檬化,其中每一行都由没有标点符号的单词组成,比如: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

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
    应用于该列的句子
  • 使用lambda表达式,该表达式获取一个
    语句
    作为输入,并以类似于在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
    应用于该列的句子
  • 使用lambda表达式,该表达式获取一个
    语句
    作为输入,并以类似于在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)])
    
    作为引理化句子(关键字使用“”):


    这个问题需要更多的注意;)(这个问题需要更多的注意;)磅