title: How to solve/what is a KeyError in Python/Pandas?
body: <p>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:</p>
  
  <pre><code>Traceback (most recent call last):
    File "E:\Holiday Project\Politic\store.py", line 19, in &lt;module&gt;
      tweets['text'] = list(map(lambda tweet: tweet['text'], tweets_data))
    File "E:\Holiday Project\Politic\store.py", line 19, in &lt;lambda&gt;
      tweets['text'] = list(map(lambda tweet: tweet['text'], tweets_data))
  KeyError: 'text'
  </code></pre>
  
  <p>and here is my code:</p>
  
  <pre><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)
  </code></pre>