How to solve/what is a KeyError in Python/Pandas? -


i have 2 text files wish work using pandas. files created in exact same way , similar, except of content inside. however, program not work 1 of text files, work other. here 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 code:

import json import pandas pd textblob import textblob  tweets_data_path = 'filename.txt'  tweets_data = [] tweets_file = open(tweets_data_path, "r") 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 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 try extract tweet['text'] not exist in of dictionaries. if "text" field exists in of lines loading, may want write that:

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

if reason, in of jsons "text" not exists, ' ' in dataframe.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -