Python新手在这里!我真的可以在我用 Python 做的一个情感项目上得到一些帮助。
我已经建立了这本字典,我想用它来浏览我从卫报文章中摘录的评论。我想在数据框中添加一个额外的列guardian_comments,表示该列中的值sentiment_score是very_negative、negative、neutral、positive 还是very_positive:
lookup_dict = {
"very_negative":[-0.9, -0.8, -0.7, -0.6],
"negative":[-0.5, -0.4, -0.3, -0.2, -0.1],
"neutral":[0],
"positive":[0.1, 0.2, 0.3, 0.4, 0.5],
"very_positive":[0.6, 0.7, 0.8, 0.9, 1]}
guardian_comments['sentiment_level'] = guardian_comments['sentiment_score'].map(lambda x: lookup_dict[x])
但是,我收到了一个KeyError我认为这取决于字典列表尚未转换为浮点数的事实。谁能建议我如何做到这一点?
谢谢!