0

I am creating datasets using data from JSON API. The program below works but the pandas datasets cannot be referenced using the object names in list (df1 or dff1) but only when they are referenced as list items: dct1(1). I would love to learn why this is happening or maybe there is an error in the code.

dct1 = ['df1', 'df2', 'df3', 'df4']
dct2 = ['dff1', 'dff2', 'dff3', 'dff4']
dct3 = ['file1', 'file2', 'file3','file4']
for i in range(0,4):
    off=1000*i
    url = 'https://data.cms.gov/data-api/v1/dataset/7b181182-828c-4fa4-91bd-641759f6eddd/data?size=1000&offset={0}'.format(off)
    dct2[i]=pd.read_json(url)
    dct3[i]= 'https://data.cms.gov/data-api/v1/dataset/7b181182-828c-4fa4-91bd-641759f6eddd/data?size=1000&offset={0}'.format(off)
    dct1[i]=pd.read_json(dct3[i])
dct1[1].head(3)
Out[64]: 
   Year State_Name  ... Avg_Risk_Score_AGND  Person_Years_AGND
0  2020   Kentucky  ...             1.08687            5509.67
1  2020   Kentucky  ...             1.05350             871.25
2  2020   Kentucky  ...             0.96509            1290.42
print(df1)
NameError: name 'df1' is not defined
print(dff1)
NameError: name 'dff1' is not defined**

1 Answers1

0

It looks like what you're actually trying to do is set the name of variables within your workspace to the strings contained in dct1 and dct2. To do that, you can do something like this.

dct1 = ['df1', 'df2', 'df3', 'df4']
dct2 = ['dff1', 'dff2', 'dff3', 'dff4']
dct3 = ['file1', 'file2', 'file3','file4']
for i in range(0,4):
    off=1000*i
    url = 'https://data.cms.gov/data-api/v1/dataset/7b181182-828c-4fa4-91bd-641759f6eddd/data?size=1000&offset={0}'.format(off)
    locals()[dct2[i]]=pd.read_json(url)
    locals()[dct3[i]]= 'https://data.cms.gov/data-api/v1/dataset/7b181182-828c-4fa4-91bd-641759f6eddd/data?size=1000&offset={0}'.format(off)
    locals()[dct1[i]]=pd.read_json(dct3[i])

However, a more conventional approach is to simply make a dictionary for which d['[name]'] has the desired value rather than assigning that value to a variable called [name].

Ben Grossmann
  • 4,387
  • 1
  • 12
  • 16
  • Thank you! This remains a really hard concept for me. Not sure if it was the most efficient thing to do in SAS either but mapping what I used to do to what I should be doing is hard! – Manas Kaushik Feb 22 '22 at 22:21