title: How can I avoid append errors using concurrent.futures in Python?
body: <p>I'm trying to create a table with the information of an API. When I analyze game by game it works fine, but when I try to analyze a large number of games using "concurrent.futures" to speed up the process it appends some wrong information to the table. Every time I run the script the errors come up in different rows, it's aleatory. </p>
  
  <p>Also, I have noticed printing the game_ids analyzed, that the script doesn't look them in order. Maybe the error is in this problem.</p>
  
  <p>How can I solve this issue? Thank you!</p>
  
  <p>This is the code I'm using.</p>
  
  <pre><code>import requests as r
  import pandas as pd
  import concurrent.futures
  
  pd.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
  
  game_id = [100, 101, 102] #This is an example, I use a large number of games
  
  d = {'game_id'        : [],
       'atbat_num'      : [],
       'play_index'     : [],
       'batter_id'      : [],
       'batter_name'    : [],
       'pitcher_id'     : [],
       'pitcher_name'   : [],
       'runner_id'      : [],
       'runner_name'    : [],
       'event'          : [],
       'start'          : [],
       'end'            : [],
       'movementReason' : []
          }
  
  def get_url(gids):
      url = (f'http://examplelink.com/str(gids)}/')
      req  = r.get(url)
      json = req.json()
  
      for i in json['allPlays']:
  
          if 'runners' in i:
  
              for p in i['runners']:
  
                  d['game_id'].append(gids)
                  if 'atBatIndex' in i:
                      d['atbat_num'].append(i['atBatIndex'])
                  else: d['atbat_num'].append(None)
                  if 'playIndex' in p['details']:
                      d['play_index'].append(p['details']['playIndex'])
                  else: d['play_index'].append(None)
                  if 'matchup' in i:
                      if 'batter' in i['matchup']:
                          d['batter_id'].append(i['matchup']['batter']['id'])                    
                      else: d['batter_id'].append(None)
                  else: d['batter_id'].append(None)
                  if 'matchup' in i:
                      if 'batter' in i['matchup']:
                          d['batter_name'].append(i['matchup']['batter']['fullName'])
                      else: d['batter_name'].append(None)
                  else: d['batter_name'].append(None)
                  if 'matchup' in i:
                      if 'pitcher' in i['matchup']:
                          d['pitcher_id'].append(i['matchup']['pitcher']['id'])
                      else: d['pitcher_id'].append(None)
                  else: d['pitcher_id'].append(None)
                  if 'matchup' in i:
                      if 'pitcher' in i['matchup']:
                          d['pitcher_name'].append(i['matchup']['pitcher']['fullName'])
                      else: d['pitcher_name'].append(None)
                  else: d['pitcher_name'].append(None)
                  if 'details' in p:
                      if 'runner' in p['details']:
                          if 'id' in p['details']['runner']:
                              d['runner_id'].append(p['details']['runner']['id'])
                          else: d['runner_id'].append(None)
                      else: d['runner_id'].append(None)
                  else: d['runner_id'].append(None)
                  if 'details' in p:
                      if 'runner' in p['details']:
                          if 'fullName' in p['details']['runner']:
                              d['runner_name'].append(p['details']['runner']['fullName'])
                          else: d['runner_name'].append(None)
                      else: d['runner_name'].append(None)
                  else: d['runner_name'].append(None)
                  if 'details' in p:
                      d['event'].append(p['details']['event'])
                  else: d['event'].append(None)
                  if 'movement' in p:
                      d['start'].append(p['movement']['start'])
                  else: d['start'].append(None)
                  if 'movement' in p:
                      d['end'].append(p['movement']['end'])
                  else: d['end'].append(None)
                  if 'details' in p:
                      d['movementReason'].append(p['details']['movementReason'])
                  else: d['movementReason'].append(None)
  
      print(f'Game {gids} analyzed')          
  
      with concurrent.futures.ThreadPoolExecutor() as executor:
          executor.map(get_url, game_id)
  
  table = pd.DataFrame(d)
  
  export_csv = table.to_csv ('runner.csv', index = None, header=True)
  
  </code></pre>