Selecting a pandas dataframe by name string generated within the code
<p>I'm writing up few lines in python where I have created 3 pandas dataframes
DF_A, dF_B, dF_C.
In the next step I do something &amp; read a string. If the string = 'A' then I want to go to dF_A(&amp; so on for B&amp;C).
Any help on how this can be done?</p>

<pre><code>DF = pd.ExcelFile('File.xlsx')
DF_A = Dev.parse("A")
DF_B = Dev.parse("B")
DF_C = Dev.parse("C")

tmp = ['B', 'A', 'C', 'A', 'A', 'B']
foo = {}

for i in range(len(tmp)):
    table = 'DF_'+tmp[i]
    foo[tmp[i]] = table[0]
</code></pre>

<p>It is possible to use <code>vars()</code>, though it's hacky</p>

<pre><code>for i in tmp:  # do not iterate by index
    table = vars()['DF_' + i]
</code></pre>

<p>It is cleaner to just store tables in a dictionary in the first place</p>

<pre><code>tables = {}
for i in ['A', 'B', 'C']:
    tables[i] = Dev.parse(i)
for i in tmp:
    table = tables['DF_' + i]
</code></pre>

python pandas