Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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:从csv中逐行提取关键字_Python_Nlp_Nltk - Fatal编程技术网

Python:从csv中逐行提取关键字

Python:从csv中逐行提取关键字,python,nlp,nltk,Python,Nlp,Nltk,我试图从csv文件中逐行提取关键字,并创建关键字字段。现在,我可以得到完整的提取。如何获取每行/字段的关键字 数据: id,some_text 1,"What is the meaning of the word Himalaya?" 2,"Palindrome is a word, phrase, or sequence that reads the same backward as forward" 代码:这是搜索整个文本,但不是逐行搜索。除了替换(r'\\124;',''),我还需要添加

我试图从csv文件中逐行提取关键字,并创建关键字字段。现在,我可以得到完整的提取。如何获取每行/字段的关键字

数据:

id,some_text
1,"What is the meaning of the word Himalaya?"
2,"Palindrome is a word, phrase, or sequence that reads the same backward as forward"
代码:这是搜索整个文本,但不是逐行搜索。除了
替换(r'\\124;','')
,我还需要添加其他内容吗

最终输出:

id,some_text,new_keyword_field
1,What is the meaning of the word Himalaya?,"meaning,word,himalaya"
2,"Palindrome is a word, phrase, or sequence that reads the same backward as forward","palindrome,word,phrase,sequence,reads,backward,forward"

这里有一个干净的方法,可以使用pandapply将新的关键字列添加到数据帧中。Apply的工作原理是首先定义一个函数(
get_keywords
,在本例中),我们可以将该函数应用于每一行或每一列

import pandas as pd
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords

# I define the stop_words here so I don't do it every time in the function below
stop_words = stopwords.words('english')
# I've added the index_col='id' here to set your 'id' column as the index. This assumes that the 'id' is unique.
df = pd.read_csv('test-data.csv', index_col='id')  
在这里,我们定义了将在下一个单元格中使用df.apply应用于每一行的函数。您可以看到,此函数
get_keywords
作为其参数,并返回一个逗号分隔的关键字字符串,就像您在上面所需的输出中一样(“含义、单词、喜马拉雅”)。在这个函数中,我们降低、标记、使用
isalpha()
过滤掉标点符号、过滤掉停止词,并将关键字连接在一起以形成所需的输出

# This function will be applied to each row in our Pandas Dataframe
# See the docs for df.apply at: 
# https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html
def get_keywords(row):
    some_text = row['some_text']
    lowered = some_text.lower()
    tokens = nltk.tokenize.word_tokenize(lowered)
    keywords = [keyword for keyword in tokens if keyword.isalpha() and not keyword in stop_words]
    keywords_string = ','.join(keywords)
    return keywords_string
现在我们已经定义了将要应用的函数,我们调用
df.apply(get_关键字,axis=1)
。这将返回熊猫系列(类似于列表)。因为我们希望这个系列成为我们数据框架的一部分,所以我们使用
df['keywords']=df.apply(get_keywords,axis=1)

输出:


这里有一个干净的方法,可以使用pandapply将新的关键字列添加到数据帧中。Apply的工作原理是首先定义一个函数(
get_keywords
,在本例中),我们可以将该函数应用于每一行或每一列

import pandas as pd
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords

# I define the stop_words here so I don't do it every time in the function below
stop_words = stopwords.words('english')
# I've added the index_col='id' here to set your 'id' column as the index. This assumes that the 'id' is unique.
df = pd.read_csv('test-data.csv', index_col='id')  
在这里,我们定义了将在下一个单元格中使用df.apply应用于每一行的函数。您可以看到,此函数
get_keywords
作为其参数,并返回一个逗号分隔的关键字字符串,就像您在上面所需的输出中一样(“含义、单词、喜马拉雅”)。在这个函数中,我们降低、标记、使用
isalpha()
过滤掉标点符号、过滤掉停止词,并将关键字连接在一起以形成所需的输出

# This function will be applied to each row in our Pandas Dataframe
# See the docs for df.apply at: 
# https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html
def get_keywords(row):
    some_text = row['some_text']
    lowered = some_text.lower()
    tokens = nltk.tokenize.word_tokenize(lowered)
    keywords = [keyword for keyword in tokens if keyword.isalpha() and not keyword in stop_words]
    keywords_string = ','.join(keywords)
    return keywords_string
现在我们已经定义了将要应用的函数,我们调用
df.apply(get_关键字,axis=1)
。这将返回熊猫系列(类似于列表)。因为我们希望这个系列成为我们数据框架的一部分,所以我们使用
df['keywords']=df.apply(get_keywords,axis=1)

输出: