用数据帧中的值填充python字典

用数据帧中的值填充python字典,python,pandas,dictionary,Python,Pandas,Dictionary,这是我的字典,叫做“评论”: 这是我的“词典”数据框架: import pandas as pd lexicon = {'word': ['like', 'movie', 'hate'], 'neg': [0.0005, 0.0014, 0.0029], 'pos': [0.0025, 0.0019, 0.0002] } lexicon = pd.DataFrame(lexicon, columns = ['word', 'neg','pos'])

这是我的字典,叫做“评论”:

这是我的“词典”数据框架:

import pandas as pd

lexicon = {'word': ['like', 'movie', 'hate'],
    'neg': [0.0005, 0.0014, 0.0029],        
    'pos': [0.0025, 0.0019, 0.0002]
    }

lexicon = pd.DataFrame(lexicon, columns = ['word', 'neg','pos'])

print (lexicon)

我需要用“词典”数据框中的neg和pos值填充我的“评论”词典

如果词典中没有值,那么我想把0.5

要最终获得这一结果:

reviews= {1: {'like': [0.0005, 0.0025], 'the': [0.5, 0.5], 'acting': [0.5, 0.5]},
          2: {'plot': [0.5, 0.5], 'hate': [0.0029, 0.0002], 'story': [0.5, 0.5]}}

lexicon
创建字典,然后通过
dict.get
在双字典理解映射中添加默认值,如果不匹配:

d = lexicon.set_index('word').agg(list, axis=1).to_dict()
print (d)
{'like': [0.0005, 0.0025], 'movie': [0.0014, 0.0019], 'hate': [0.0029, 0.0002]}


out = {k: {x: d.get(x, [0.5,0.5]) for x in v} for k, v in reviews.items()}
print (out)
{1: {'like': [0.0005, 0.0025], 'the': [0.5, 0.5], 'acting': [0.5, 0.5]}, 
 2: {'story': [0.5, 0.5], 'hate': [0.0029, 0.0002], 'plot': [0.5, 0.5]}}
你可以在这里用

d = lexicon.set_index('word').agg(list, axis=1).to_dict()
print (d)
{'like': [0.0005, 0.0025], 'movie': [0.0014, 0.0019], 'hate': [0.0029, 0.0002]}


out = {k: {x: d.get(x, [0.5,0.5]) for x in v} for k, v in reviews.items()}
print (out)
{1: {'like': [0.0005, 0.0025], 'the': [0.5, 0.5], 'acting': [0.5, 0.5]}, 
 2: {'story': [0.5, 0.5], 'hate': [0.0029, 0.0002], 'plot': [0.5, 0.5]}}
df_ = lexicon.set_index("word").agg(list, axis=1)
out = {k: df_.reindex(v, fill_value=[0.5, 0.5]).to_dict() for k, v in reviews.items()}

# {1: {'the': [0.5, 0.5], 'like': [0.0005, 0.0025], 'acting': [0.5, 0.5]},
# 2: {'story': [0.5, 0.5], 'hate': [0.0029, 0.0002], 'plot': [0.5, 0.5]}}