title: How do I return multiple pandas dataframes with unique names from a for loop?
body: <p>I'm trying to use a for loop to create multiple dataframes. I see the output I want if I put a <code>print()</code> output inside the for loop. However, when I change that to a <code>return</code> output to access the dataframes from outside the function, I'm only able to get the first dataframe. I'm confused about what's happening and I would appreciate your help!</p>
  
  <p>Code:</p>
  
  <pre><code># Global libraries
  from datetime import datetime
  import pandas as pd
  
  # Local libraries
  from instruments import metadata    
  
  # Get the data
  def data():
  
      # Get list of instruments
      nifty = metadata()
  
      # Specify the date range for the data pull
      from_date = datetime.today().strftime('%Y-%m-%d 09:00:00')
      to_date = datetime.today().strftime('%Y-%m-%d 15:30:00')
  
      # Interval is the candle interval (minute, day, 5 minute etc.).
      interval = 'minute'
  
      # Iterate through the metadata
      for stock in nifty:
  
          # Set the instrument token for the data pull
          instrument_token = stock['instrument_token']
  
          # Call the api (I've removed login information for kite from this script) 
          hist =  kite.historical_data(instrument_token,
                                      from_date,
                                      to_date,
                                      interval,
                                      continuous=False,
                                      oi=False)
  
  
          # Put the data in a pandas dataframe as a timeseries
          data = pd.DataFrame(hist).set_index('date')
  
  
          # Format data
          data.columns = map(str.capitalize, data.columns)
          data['Expiry'] = stock['expiry']
          data = data[['Open','High','Low','Close','Volume','Expiry']]
          data.index.names = ['Datetime']
  
          # return data as a print statement - this works and I see the correct output
          print(data)
  
          # return data - this does not work as intended. If I call the function, I only get the first dataframe 
          return data
  </code></pre>
  
  <p>Sample data for the variable <code>nifty</code>:</p>
  
  <pre><code>[{'tick_size': 0.05, 'expiry': datetime.date(2020, 4, 30), 'exchange_token': '56059', 'instrument_type': 'FUT', 'segment': 'NFO-FUT', 'strike': 0.0, 'last_pri
  ce': 0.0, 'name': 'NIFTY', 'lot_size': 75, 'tradingsymbol': 'NIFTY20APRFUT', 'exchange': 'NFO', 'instrument_token': 14351106}, {'tick_size': 0.05, 'expiry': d
  atetime.date(2020, 6, 25), 'exchange_token': '95734', 'instrument_type': 'FUT', 'segment': 'NFO-FUT', 'strike': 0.0, 'last_price': 0.0, 'name': 'NIFTY', 'lot_
  size': 75, 'tradingsymbol': 'NIFTY20JUNFUT', 'exchange': 'NFO', 'instrument_token': 24507906}]
  </code></pre>
  
  <p>Sample data in the variable <code>hist</code>:</p>
  
  <pre><code>[{'volume': 220650, 'low': 9158.45, 'close': 9173.7, 'date': datetime.datetime(2020, 4, 23, 9, 15, tzinfo=tzoffset(None, 19800)), 'high': 9200, 'open': 9200},
   {'volume': 92475, 'low': 9173, 'close': 9176.75, 'date': datetime.datetime(2020, 4, 23, 9, 16, tzinfo=tzoffset(None, 19800)), 'high': 9180, 'open': 9173}]
  </code></pre>
  
  <p>I've been reading about giving dynamic names to the dataframes to make them unique but I'm lost as to how to make that work.</p>
  
  <p>Thanks in advance for your help!</p>