Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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 如何迭代存储在dataframe中的文本以提取句子并在循环中查找值?_Python_Pandas_Loops - Fatal编程技术网

Python 如何迭代存储在dataframe中的文本以提取句子并在循环中查找值?

Python 如何迭代存储在dataframe中的文本以提取句子并在循环中查找值?,python,pandas,loops,Python,Pandas,Loops,我把文本存储在一个包含许多句子的数据框中。我已经编写了一个单独的函数,其中我在一个句子中查找某些关键字和值,并希望能够将这些值存储在同一数据帧的不同列中。当我迭代数据帧的行以首先标记每个句子时,我遇到了一个问题 当我将显式语句传递给函数时,这就起作用了。我的问题是当我试图在循环中把文本标记成句子时。我在rf[“Nod_size”]中得到空结果。然而,“2.9x1.7”和“2.5x1.3”是我的预期结果 这是我正在使用的代码 import pandas as pd import numpy a

我把文本存储在一个包含许多句子的数据框中。我已经编写了一个单独的函数,其中我在一个句子中查找某些关键字和值,并希望能够将这些值存储在同一数据帧的不同列中。当我迭代数据帧的行以首先标记每个句子时,我遇到了一个问题

当我将显式语句传递给函数时,这就起作用了。我的问题是当我试图在循环中把文本标记成句子时。我在rf[“Nod_size”]中得到空结果。然而,“2.9x1.7”和“2.5x1.3”是我的预期结果

这是我正在使用的代码

 import pandas as pd
 import numpy as np
 import nltk
 import re
 from nltk.tokenize import TweetTokenizer, sent_tokenize, word_tokenize

 rf = pd.DataFrame([{"Text": "CHEST CA lung. -Increased sizes of nodules in RLL. There is further increased size and solid component of part-solid nodule associated with internal bubbly lucency and pleural tagging at apicoposterior segment of the LUL (SE 3; IM 38-50), now measuring about 2.9x1.7 cm in greatest transaxial dimension (previously size 2.5x1.3 cm in 2015).", "Stage": "T2aN2M0"},
               {"Text": "CHEST CA lung. Post LL lobectomy. As compared to study obtained on 30/10/2018, -Top normal heart size. -Increased sizes of nodules in RLL.", "Stage": "T2aN2M0"}])

 nodule_keywords = ["nodules","nodule"]
 nodule_length_keyword = ["cm","mm", "centimeters", "milimeters"]

 def GetNodule(sentence):
     sentence = re.sub('-', ' ', sentence)
     token_words = nltk.word_tokenize(sentence)
     df = pd.DataFrame(token_words)
     df['check_nodkeywords'] = df[0].str.lower().isin(nodule_keywords)
     df['check_nod_len_keywords'] = 
     df[0].str.lower().isin(nodule_length_keyword)
     check = np.any(df['check_nodkeywords']==True)
     check1 =np.any(df['check_nod_len_keywords']==True)
     if ((check==True)&(check1==True)):
          position = np.where(df['check_nod_len_keywords']==True)
          position = position[0]
          nodule_size = df[0].iloc[position-1]
          return nodule_size

 for sub_list in rf['Text']:
     sent = sent_tokenize(str(sub_list))
     for sub_sent_list in sent:
         result_calcified_nod = GetNodule(sub_sent_list)
         rf["Nod_size"] = result_calcified_nod 

请帮忙!!我认为这是一个概念问题,而不是编程问题。请帮我解决

以下代码应满足您的要求

rf["Nod_size"] = ""
for i,sub_list in zip(range(len(rf)),rf['Text']):
    temp = []
    for sentence in sent_tokenize(sub_list):
        result_calcified_nod = GetNodule(sentence)
        temp.append(result_calcified_nod)
    rf.loc[i]["Nod_size"] = temp

你能
在for循环内打印(键入(sub_列表))
并查看输出内容吗?@Vishal这是我在for循环内打印(键入(sub_列表))时得到的结果。你能尝试使用sent_标记化(str(sub_列表))吗?还有,对于任何特定的子列表,您是否有任何空白或NaN或无?要在for循环中检查此
print(sub_list)
。我尝试使用sub_list=None和sublist=np.nan,得到了相同的错误。当我插入str时,仍然有相同的错误:sent_tokenize(str(sub_list))。当我打印(子列表)时,它会按原样提供数据行的文本。我只尝试了两行第一,没有丢失的数据。我需要句子标记,以确保我寻找的两个关键字存在于同一个句子。如果我不这样做,我会得到假阳性结果,因为它需要任何长度作为结节长度。它将接受文本中任何位置的第一个关键字。