Paris Spinelli
Paris Spinelli

Reputation: 5

Creating n pandas dataframes based on user input in python

Pretty new to python so any help is appreciated. I am trying to generate multiple pandas dataframes from separate excel spreadsheets. However when I try to reference the dataframe later it returns as a list:

what I am trying is this

ref_dict = {}

... code to get the files open etc ...
        
        foo = []
        bar = []
        goo = []
        tar = []
        
        for z in range(40,c):
            foo_v = wb[n].cell(row=z, column=2).value
            bar_v = wb[n].cell(row=z, column=3).value
            goo_v = wb[n].cell(row=z, column=4).value
            tar_v = wb[n].cell(row=z, column=5).value
            foo.append(foo_v)
            bar.append(bar_v)
            goo.append(goo_v)
            tar.append(tar_v)
            d = {'Foo': foo, 'Bar': bar, 'Goo': goo, 'Tar': tar}
            df = pd.DataFrame(d)
            ref_dict[unique_ID] = []
            ref_dict[unique_ID] = [df]

.... later on ...

for i in ref_dict:
    print(type(ref_dict[i]))

The print type returns as a list and not a data frame - thus I cannot call columns etc. I could store multiple keys in a dict and assemble the dataframes later but it seems hacky and there should be a better way to do this?

Upvotes: 0

Views: 237

Answers (1)

ProblemsLoop
ProblemsLoop

Reputation: 540

As mentioned in Michael's comment above, you assigned [df] as a value in your dictionary. If you want dataframes to be stored, then store them directly.

ref_dict[unique_ID] = df

If you really want them to be lists, then just access them like a list in a dictionary.

print(type(ref_dict[i][0]))

Upvotes: 1

Related Questions