title: How do I add a dynamic list of variable to the command pd.concat
body: <p>I am using python3 and pandas to create a script that will:</p>
  
  <ul>
  <li>Be dynamic across different dataset lengths(rows) and unique values - completed</li>
  <li>Take unique values from column A and create separate dataframes as variables for each unique entry - completed</li>
  <li>Add totals to the bottom of each dataframe - completed</li>
  <li>Concatenate the separate dataframes back together - incomplete</li>
  </ul>
  
  <p>The issue is I am unable to formulate a way to create a list of the variables in use and apply them as arg in to the command pd.concat.</p>
  
  <p>The sample dataset. The dataset may have more unique BrandFlavors or less which is why the script must be flexible and dynamic.</p>
  
  <p><a href="https://i.stack.imgur.com/TxCF2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TxCF2.png" alt="enter image description here"></a></p>
  
  <p>Script:</p>
  
  <pre><code>import pandas as pd
  import warnings
  warnings.simplefilter(action='ignore')
  excel_file = ('testfile.xlsx')
  df = pd.read_excel(excel_file)
  df = df.sort_values(by='This', ascending=False)
  colarr = df.columns.values
  arr = df[colarr[0]].unique()
  for i in range(len(arr)):
      globals()['var%s' % i] = df.loc[df[colarr[0]] == arr[i]]
  for i in range(len(arr)):
      if globals()['var%s' % i].empty:
          ''
      else: 
          globals()['var%s' % i] = globals()['var%s' % i].append({'BrandFlavor':'Total',
                                  'This':globals()['var%s' % i]['This'].sum(),
                                  'Last':globals()['var%s' % i]['Last'].sum(),
                                  'Diff':globals()['var%s' % i]['Diff'].sum(),
                                  '% Chg':globals()['var%s' % i]['Diff'].sum()/globals()['var%s' % i]['Last'].sum() * 100}, ignore_index=True)
          globals()['var%s' % i]['% Chg'].fillna(0, inplace=True)
          globals()['var%s' % i].fillna(' ', inplace=True)
  </code></pre>
  
  <p>I have tried this below, however the list is a series of strings</p>
  
  <pre><code>vararr = []
  count = 0
  for x in range(len(arr)):
      vararr.append('var' + str(count))
      count = count + 1
  df = pd.concat([vararr])
  </code></pre>
  
  <p>pd.concat does not recognize a string. I tired to build a class with an arg defined but had the same issue.</p>
  
  <p>The desired outcome would be a code snippet that generated a list of variables that matched the ones created by lines 9/10 and could be referenced by pd.concat([<em>list, of, vars, here</em>]). It must be dynamic. Thank you</p>