简体   繁体   中英

what is a KeyError in Python 3.5/Pandas?

I work with files were exported from stream twitter . but when run code then error : KeyError: 'text' on tweets['python'] = tweets['text'].apply(lambda tweet: word_in_text('python', tweet)). 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. Try: tweets = pd.DataFrame(data=tweets_data, columns=['tweets'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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