[英]what is a KeyError in Python 3.5/Pandas?
I work with files were exported from stream twitter . 我使用的文件是从流twitter导出的。 but when run code then error : KeyError: 'text' on tweets['python'] = tweets['text'].apply(lambda tweet: word_in_text('python', tweet)).
但是当运行代码时出现错误:KeyError:tweets ['python'] = tweets ['text']。apply(lambda tweet:word_in_text('python',tweet))上的'text'。 how to fix it ?
如何解决呢? thank everyone..!.
谢谢大家..!
import re import json import string import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib tweets_data_path = '...\\\\..\\\\log1000.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 def word_in_text(word, text): word = word.lower() text = text.lower() match = re.search(word, text) if match: return True return False #------------------------DataFrame-------------------------- tweets = pd.DataFrame() #------------------------------------------------------------------------ tweets['python'] = tweets['text'].apply(lambda tweet: word_in_text('python', tweet)) #---------------------------------------------------------------- print (tweets['python'].value_counts()[True])
密钥错误表示在字典/数据框中找不到具有这种密钥的数据。
There isn't any data in you DataFrame. 您的DataFrame中没有任何数据。 Try:
tweets = pd.DataFrame(data=tweets_data, columns=['tweets'])
尝试:
tweets = pd.DataFrame(data=tweets_data, columns=['tweets'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.