Skip to content Skip to sidebar Skip to footer

Assigning Variables To Dataframes Dynamically - Error

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('displ

Solution 1:

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]

forstate 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

Post a Comment for "Assigning Variables To Dataframes Dynamically - Error"