0

I am attempting to loop through a list of states to assign relevant variable names to dataframes of data. Doing so, I encountered an error.

import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
import numpy as np


states = ['az', 'ca']
relative_path = './Data/censusdata_'


for state in states:
    df_name = 'data_' + state
  
    data_path = relative_path + state + '.csv'
    dataframe = pd.read_csv(data_path, encoding='latin-1')
   
    exec(f'{df_name}={dataframe}')

print(data_ca)

The error:

Traceback (most recent call last):
  File ".\varnametest.py", line 27, in <module>
    exec(f'{df_name}={dataframe}')
  File "<string>", line 1
    data_az=     SUMLEV  STATE  COUNTY  PLACE  COUSUB  CONCIT  PRIMGEO_FLAG FUNCSTAT  \
                         ^
SyntaxError: invalid syntax

1 Answers1

0

If you want to create a dynamic dataframe, you need to use globals.

Here's how I would do it.

import pandas as pd
import numpy as np
glb = globals()
states = ['az', 'ca']

for state in states:
    df_name = 'data_' + state
  
    dataframe = pd.DataFrame({state:[1,2,3,4,5]})
   
    glb['data_' + state] = dataframe

    #or you can also give
    #glb[df_name] = dataframe

    #exec("f'{df_name}=dataframe'")

print(data_az)
print(data_ca)

The results of this will be:

print(data_az) will result in:

   az
0   1
1   2
2   3
3   4
4   5

print(data_ca) will result in:

   ca
0   1
1   2
2   3
3   4
4   5

To make it even more interesting for you, see the below example:

import pandas as pd
glb = globals()
states = ['az', 'ca']

az_data = [1,2,3,4,5]
ca_data = [6,7,8,9,0]

for state in states:
    df_name = 'data_' + state

    df_data = state + '_data'
  
    dataframe = pd.DataFrame({state:glb[df_data]})
   
    glb[df_name] = dataframe
    #exec("f'{df_name}=dataframe'")

print(data_az)
print(data_ca)

Output of this will be:

print (data_az) will give you:

   az
0   1
1   2
2   3
3   4
4   5

print (data_ca) will give you:

   ca
0   6
1   7
2   8
3   9
4   0
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33