簡體   English   中英

在 PYTHON 2.7 中使用 Pandas 時出現 Keyerror

[英]Keyerror while using pandas in PYTHON 2.7

7 在命令窗口上運行它時,我遇到了這個問題。 我還更新了熊貓,我已經仔細檢查了所有內容,請幫助我解決這個問題。 我有我正在使用的 Twitter 數據,這些數據是通過 Tweepy 生成的。

import json
import pandas as pd
import matplotlib.pyplot as plt

tweets_data_path = 'C:/Users/e2sn7cy/Documents/GitHub/twitter_data.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)

#DataFrame
tweets = pd.DataFrame()

#adding columns

tweets['text'] = map(lambda tweet: tweet['text'], tweets_data)

tweets['lang'] = map(lambda tweet: tweet['lang'], tweets_data)

tweets['country'] = map(lambda tweet: tweet['place']['country'] if tweet['place'] != None else None, tweets_data)

#Adding Charts
tweets_by_lang = tweets['lang'].value_count()

fig, ax = plt.subplots()
ax.tick_params(axis='x', labelsize=15)
ax.tick_params(axis='y', labelsize=10)
ax.set_xlabel('Languages', fontsize=15)
ax.set_ylabel('Number of tweets' , fontsize=15)
ax.set_title('Top 5 languages', fontsize=15, fontweight='bold')
tweets_by_lang[:5].plot(ax=ax, kind='bar', color='red')

錯誤:

(venv) C:\Users\e2sn7cy\Documents\GitHub\Twitter-App>python twitter_analytics.py
Traceback (most recent call last):
File "twitter_analytics.py", line 25, in <module>
tweets['text'] = map(lambda tweet:tweet['text'] if tweet['text'] else '', tweets_data)
File "twitter_analytics.py", line 25, in <lambda>
tweets['text'] = map(lambda tweet:tweet['text'] if tweet['text'] else '', tweets_data)
KeyError: 'text'

推特數據

{"created_at":"Wed Jun 10 10:01:16 +0000 2015","id":608574643897778176,"id_str":"608574643897778176","text":"D\u00e9couvrez le TOP 10 des plus beaux buts de la Ligue des Champions ! #LDC \n\nVid\u00e9o &gt; http:\/\/t.co\/qrOS6JGBYa http:\/\/t.co\/gSBzkhpdWQ","source":"\u003ca href=\"https:\/\/about.twitter.com\/products\/tweetdeck\" rel=\"nofollow\"\u003eTweetDeck\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":18802382,"id_str":"18802382","name":"Football.fr","screen_name":"football_fr","location":"","url":"http:\/\/www.football.fr","description":"Compte Twitter officiel de Football.fr, site d'actualit\u00e9s de Newsweb Lagard\u00e8re Active.","protected":false,"verified":true,"followers_count":15012,"friends_count":1290,"listed_count":165,"favourites_count":30,"statuses_count":19099,"created_at":"Fri Jan 09 14:35:24 +0000 2009","utc_offset":7200,"time_zone":"Paris","geo_enabled":true,"lang":"fr","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/403703872\/h_h_h_h_.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/403703872\/h_h_h_h_.png","profile_background_tile":false,"profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/562565984548970496\/ggwgwYbh_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/562565984548970496\/ggwgwYbh_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/18802382\/1433626125","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"LDC","indices":[68,72]}],"trends":[],"urls":[{"url":"http:\/\/t.co\/qrOS6JGBYa","expanded_url":"http:\/\/www.football.fr\/ligue-des-champions\/articles\/ronaldo-messi-benzema-les-10-plus-beaux-buts-de-la-saison-video-668464\/","display_url":"football.fr\/ligue-des-cham\u2026","indices":[86,108]}],"user_mentions":[],"symbols":[],"media":[{"id":608574518903341056,"id_str":"608574518903341056","indices":[109,131],"media_url":"http:\/\/pbs.twimg.com\/media\/CHIXTPKWgAAqSXS.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/CHIXTPKWgAAqSXS.jpg","url":"http:\/\/t.co\/gSBzkhpdWQ","display_url":"pic.twitter.com\/gSBzkhpdWQ","expanded_url":"http:\/\/twitter.com\/football_fr\/status\/608574643897778176\/photo\/1","type":"photo","sizes":{"small":{"w":340,"h":226,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":600,"h":400,"resize":"fit"},"large":{"w":900,"h":600,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":608574518903341056,"id_str":"608574518903341056","indices":[109,131],"media_url":"http:\/\/pbs.twimg.com\/media\/CHIXTPKWgAAqSXS.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/CHIXTPKWgAAqSXS.jpg","url":"http:\/\/t.co\/gSBzkhpdWQ","display_url":"pic.twitter.com\/gSBzkhpdWQ","expanded_url":"http:\/\/twitter.com\/football_fr\/status\/608574643897778176\/photo\/1","type":"photo","sizes":{"small":{"w":340,"h":226,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":600,"h":400,"resize":"fit"},"large":{"w":900,"h":600,"resize":"fit"}}}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"fr","timestamp_ms":"1433930476532"}

{"created_at":"Wed Jun 10 10:01:16 +0000 2015","id":608574645676204032,"id_str":"608574645676204032","text":"#Coach Training Manager opportunity with Lawn #Tennis Association http:\/\/t.co\/eLUBYQVJZj","source":"\u003ca href=\"http:\/\/www.hootsuite.com\" rel=\"nofollow\"\u003eHootsuite\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":191782743,"id_str":"191782743","name":"Jump in Sport","screen_name":"jumpinsport","location":"Lausanne, Switzerland ","url":"http:\/\/www.jumpinsport.com\/","description":"We collect the best sports related jobs from top sources around the world and show them all in one place! Follow us for great job opportunities.","protected":false,"verified":false,"followers_count":8279,"friends_count":882,"listed_count":168,"favourites_count":38,"statuses_count":30202,"created_at":"Fri Sep 17 10:18:02 +0000 2010","utc_offset":7200,"time_zone":"Bern","geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"DBE9ED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/215611005\/logo_jump_3_square__4_.jpeg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/215611005\/logo_jump_3_square__4_.jpeg","profile_background_tile":false,"profile_link_color":"CC3366","profile_sidebar_border_color":"DBE9ED","profile_sidebar_fill_color":"E6F6F9","profile_text_color":"333333","profile_use_background_image":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1267864780\/logo_jump_3_square__4__normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1267864780\/logo_jump_3_square__4__normal.jpeg","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"Coach","indices":[0,6]},{"text":"Tennis","indices":[46,53]}],"trends":[],"urls":[{"url":"http:\/\/t.co\/eLUBYQVJZj","expanded_url":"http:\/\/www.jumpinsport.com\/?q=node\/30549","display_url":"jumpinsport.com\/?q=node\/30549","indices":[66,88]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1433930476956"}

這是我生成此 Twitter 數據的程序

#Import important methods from tweepy

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

#Definig variables for Twitter Credentials

access_token = "ABC"
access_token_secret = "XYZ"
consumer_key = "A123"
consumer_secret = "Z123"

#StdOut received tweets

class StdOutListener(StreamListener):

def on_data(self,data):
    print data
    return True

def on_error(self,status):
    print status

if __name__ == '__main__':

   #Twitter authentication and connection

   l = StdOutListener()
   auth = OAuthHandler(consumer_key, consumer_secret)
   auth.set_access_token(access_token, access_token_secret)
   stream = Stream(auth, l)

   #Filter Twitter Streams by Keywords
   stream.filter(track=['cricket','football','tennis'])

在此之后,我使用此命令來捕獲 txt 文件中的數據

   python twitter_streaming.py > twitter_data.txt

這樣做沒有問題:

import pandas as pd
aux = pd.DataFrame()
aux['text'] = [0, 1, 2]

所以問題在於你的tweet['text'] (在map函數內):

tweets['text'] = map(lambda tweet: tweet['text'], tweets_data)

在(至少)您從文件tweets_data_path讀取並使用json加載的那些行之一中,沒有這樣的字段'text'

另外,作為旁注,請考慮將列表推導式作為maplambda的替代方案:

array = [{'a': 1, 'b': 2}, {'a': 11, 'b': 22}]
map(lambda x: x['a'], array)
[x['a'] for x in array]

看看最后兩行如何輸出相同的數組,盡管我個人認為后者更容易閱讀。

更新(解決方法)

認為上面的信息足以了解問題出在哪里以及如何自己找到解決方案(在學習編程語言時,我認為邊做邊學總是更好)。

根據您的要求,這里有一個解決方法:如果行不完整,您可以避免在從文件加載tweets_data文附加到tweets_data 就像是:

for line in tweets_file:
    tweet = json.loads(line)
    if not all(x in tweet for x in ['text', 'lang', 'place']):
        continue
    if tweet['place'] and not 'country' in tweet['place']:
        continue
    tweets_data.append(tweet)

請注意,代碼尚未經過測試。 嘗試了解它的工作原理並修復它或使其適應您的需求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM