Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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 如何从循环中的字典填充数据帧_Python_Pandas_Dictionary_Google Natural Language - Fatal编程技术网

Python 如何从循环中的字典填充数据帧

Python 如何从循环中的字典填充数据帧,python,pandas,dictionary,google-natural-language,Python,Pandas,Dictionary,Google Natural Language,我正在尝试对文本执行实体分析,并希望将结果放入数据框中。目前,结果既不存储在字典中,也不存储在数据帧中。使用两个函数提取结果 df: 我有以下代码: result = entity_analysis(df, 'neg_arg', 'ID') #This code loops through the rows and calls the function entities_text() def entity_analysis(df, col, idcol): temp_dict = {}

我正在尝试对文本执行实体分析,并希望将结果放入数据框中。目前,结果既不存储在字典中,也不存储在数据帧中。使用两个函数提取结果

df:

我有以下代码:

result = entity_analysis(df, 'neg_arg', 'ID')

#This code loops through the rows and calls the function entities_text()
def entity_analysis(df, col, idcol):
    temp_dict = {}
    for index, row in df.iterrows():
        id = (row[idcol])
        x = (row[col])
        entities = entities_text(x, id)
        #temp_dict.append(entities)
    #final = pd.DataFrame(columns = ['id', 'name', 'type', 'salience'])
    return print(entities)

def entities_text(text, id):
    """Detects entities in the text."""
    client = language.LanguageServiceClient()
    ent_df = {}
    if isinstance(text, six.binary_type):
        text = text.decode('utf-8')

    # Instantiates a plain text document.
    document = types.Document(
        content=text,
        type=enums.Document.Type.PLAIN_TEXT)

    # Detects entities in the document.
    entities = client.analyze_entities(document).entities

    # entity types from enums.Entity.Type
    entity_type = ('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION',
                   'EVENT', 'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER')

    for entity in entities:
        ent_df[id] = ({
            'name': [entity.name],
            'type': [entity_type[entity.type]],
            'salience': [entity.salience]
        })
    return print(ent_df)
该代码给出了以下结果:

{'132': {'name': ['management'], 'type': ['OTHER'], 'salience': [0.16079013049602509]}}
{'132': {'name': ['leadership'], 'type': ['OTHER'], 'salience': [0.05074194446206093]}}
{'132': {'name': ['salary'], 'type': ['OTHER'], 'salience': [0.27505040168762207]}}
{'145': {'name': ['days'], 'type': ['OTHER'], 'salience': [0.004272154998034239]}}
我已经在函数
entity\u analysis()
中创建了
temp\u dict
final
数据帧。解释了在循环中附加到数据帧是无效的我不知道如何有效地填充数据帧。与我的问题相关,但它们解释了如何从现有数据填充数据帧。当我尝试使用
temp_dict.update(entities)
并返回
temp_dict
时,我得到一个错误:

在实体分析中 临时指令更新(实体) TypeError:“非类型”对象不可编辑

我希望输出如下:

ID          name                  type                salience
132         management            OTHER               0.16079013049602509 
132         leadership            OTHER               0.05074194446206093 
132         salary                OTHER               0.27505040168762207 
145         days                  OTHER               0.004272154998034239 

一种解决方案是通过
实体创建列表列表。然后将列表列表馈送到
pd.DataFrame

LoL = []

for entity in entities:
    LoL.append([id, entity.name, entity_type[entity.type], entity.salience])

df = pd.DataFrame(LoL, columns=['ID', 'name', 'type', 'salience'])

如果还需要当前生成的格式的词典,则可以将当前逻辑添加到
for
循环中。但是,首先检查是否需要使用两种结构来存储相同的数据。

您刚才是否将问题从“当前结果未存储在词典中”更改为“当前结果存储在词典中”?这对你原来的问题来说是一个很大的改变,我认为它应该被回滚,因为已经有了答案。谢谢你的回答,我已经找到了一个解决方案。我创建了多个词典,因为我必须反复阅读文本和单词。为这一混乱道歉。
LoL = []

for entity in entities:
    LoL.append([id, entity.name, entity_type[entity.type], entity.salience])

df = pd.DataFrame(LoL, columns=['ID', 'name', 'type', 'salience'])