简体   繁体   English

如何解决/ Python / Pandas中的KeyError是什么?

[英]How to solve/what is a KeyError in Python/Pandas?

I have two text files that I wish to work with using Pandas. 我有两个文本文件,我希望使用Pandas。 The files were created in the exact same way and are very similar, except for some of the content inside. 除了内部的一些内容之外,文件以完全相同的方式创建并且非常相似。 However, my program does not work with one of the text files, but does work with the other. 但是,我的程序不能与其中一个文本文件一起使用,但可以与另一个一起使用。 Here is my error: 这是我的错误:

Traceback (most recent call last):
  File "E:\Holiday Project\Politic\store.py", line 19, in <module>
    tweets['text'] = list(map(lambda tweet: tweet['text'], tweets_data))
  File "E:\Holiday Project\Politic\store.py", line 19, in <lambda>
    tweets['text'] = list(map(lambda tweet: tweet['text'], tweets_data))
KeyError: 'text'

and here is my code: 这是我的代码:

import json
import pandas as pd
from textblob import TextBlob

tweets_data_path = 'filename.txt'

tweets_data = []
tweets_file = open(tweets_data_path, "r")
for line in tweets_file:
    try:
        tweet = json.loads(line)
        tweets_data.append(tweet)
    except:
        continue

print (len(tweets_data))

tweets = pd.DataFrame()
tweets['text'] = list(map(lambda tweet: tweet['text'], tweets_data))
tweets['lang'] = list(map(lambda tweet: tweet['lang'], tweets_data))
tweets['country'] = list(map(lambda tweet: tweet['place']['country'] if tweet['place'] != None else None, tweets_data))
avg = 0
for lol in tweets['text']:
    tweet = TextBlob(text)
    avg = tweet.sentiment.polarity + avg
avg = avg/len(tweets)
print(avg)

look on these lines: 看看这些方面:

tweets = pd.DataFrame()
tweets['text'] = list(map(lambda tweet: tweet['text'], tweets_data))

You probably try to extract tweet['text'] which does not exist in some of the dictionaries. 您可能尝试提取某些词典中不存在的推文['text'] If the "text" field exists in only some of the lines you are loading, than you may want to write something like that: 如果“text”字段仅存在于您正在加载的某些行中,那么您可能希望编写类似的内容:

tweets = pd.DataFrame()
tweets['text'] = [tweet.get('text','') for tweet in tweets_data]
tweets['lang'] = [tweet.get('lang','') for tweet in tweets_data]
#and so on...

If for some reason, in some of the jsons "text" do not exists, you will get ' ' in the DataFrame. 如果出于某种原因,在某些jsons“text”中不存在,那么你将在DataFrame中得到''。

tweet['text'] does not seem to exist. 推文['text']似乎不存在。 A key error is generated when you try to access a key in a hash map/dictionary that does not exist. 当您尝试访问不存在的哈希映射/字典中的密钥时,会生成密钥错误。 for example 例如

myDict = {"hello": 1, "there": 2}
print myDict["hello"] #this prints 1
print myDict["friend"] #this will generate a key error because it does not exist

您应该添加与上一类别相同的条件,以跳过“无文本”情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM