I have a python dictionary containing 3 dataframes and nothing else. I need to call each dataframe by dataframe name without using d['']; for example, with the dataframe loopdata1, I need to call it without doing d['loopdata1']. Here's the dictionary with the 3 dataframes loopdata1, loopdata2, and loopdata3:
dict_items([('loopdata1', index id name age sex sterilized
0 0 A006100 Scamp 120 0 1
1 1 A047759 Oreo 120 0 1
2 2 A134067 Bandit 192 0 1
3 3 A141142 Bettie 180 1 1
4 4 A163459 Sasha 180 1 0
5 5 A165752 Pep 180 0 1
6 6 A178569 Boti 180 0 1
7 7 A189592 Ophelia 216 1 1
8 8 A191351 Bri-Bri 192 1 0
9 9 A197810 Sassafrass 168 1 1),
('loopdata2', index id name age sex sterilized
0 0 A200922 Carlos 192 0 1
1 1 A208755 Kootrie 168 0 1
2 2 A210457 Caleb 204 0 1
3 3 A212672 Cujo 156 1 0
4 4 A214991 Prissy 228 1 1
5 5 A215368 Guiness 156 0 1
6 6 A218622 Oliver 180 0 1
7 7 A218624 Cookie 180 0 1
8 8 A221174 Lippy 216 1 1
9 9 A221327 Jamie 192 1 1),
('loopdata3', index id name age sex sterilized
0 0 A249087 *Polly 180 1 1
1 1 A251095 Beauty 168 1 1
2 2 A251214 Rex 144 0 1
3 3 A251268 Sully 204 0 1
4 4 A251402 Amy 216 1 1
5 5 A253939 Dirty 144 1 1
6 6 A254503 Daisy 204 1 1
7 7 A256412 Beau 192 0 0
8 8 A258441 Spring 168 1 1
9 9 A260631 Popki 168 0 1)])
Here's the code that generated the dictionary -- I'm importing excel files that have the same names as the dataframes and stripping off the '.xlsx':
import os
import glob
import pandas as pd
my_dir = '../test/'
os.chdir( my_dir )
filelist = []
for files in glob.glob( '*.xlsx' ) :
filelist.append(files)
lst = [os.path.splitext(x)[0] for x in filelist]
lst
d = {}
for dfname in lst:
d[dfname] = pd.read_excel(dfname + '.xlsx')
I've tried Convert a dictionary to a pandas dataframe and Extracting dataframes from a dictionary of dataframes with no luck. Thanks for taking a look!