如何解决/什么是 Python/Pandas 中的 KeyError?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31274243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve/what is a KeyError in Python/Pandas?
提问by fosho
I have two text files that I wish to work with using 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:
我有两个文本文件希望使用 Pandas 处理。这些文件以完全相同的方式创建并且非常相似,除了其中的一些内容。但是,我的程序不适用于其中一个文本文件,但适用于另一个。这是我的错误:
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)
回答by Dror Hilman
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. If the "text" field exists in only some of the lines you are loading, than you may want to write something like that:
您可能尝试提取某些词典中不存在的tweet['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 中“文本”不存在,您将在 DataFrame 中得到 ' '。
回答by Jeff H.
tweet['text'] does not seem to exist. A key error is generated when you try to access a key in a hash map/dictionary that does not exist. for example
tweet['text'] 似乎不存在。当您尝试访问不存在的哈希映射/字典中的键时,会生成键错误。例如
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
回答by Ramin Taghizada
You should add the same condition as you did for the last category to skip "None text" case .
您应该添加与为最后一个类别所做的相同的条件以跳过 "None text" case 。

