Iteratively Output Individual Dataframes from a Dictionary of Dataframes (Python)

Viewed 54

First, I have a dictionary of dataframes named (dfs). There are five dataframes in the dictionary.

Second, using the the dictionary of dataframes (dfs), I run the code below to classify items in the dataframes, dfs, and save them to a new dictionary of dataframes (out_dfs), see sample output below.

out_dfs = {}
for idx, data in enumerate(list(dfs.values())):
    out_dfs[idx] = clf.(data)
out_dfs


{0: array([-0.01126357,  0.01003442, -0.00631642, -0.00631642,  0.01589343,
         0.00999904,  0.05703701,  0.01549907,  0.01364896,  0.04490213]),
 1: array([-0.01126357, -0.00631642,  0.01572133,  0.01465883,  0.05006177,
         0.0127623 ,  0.00754415,  0.05572284,  0.00996258,  0.00618012,]),
 2: array([-0.01126357,  0.0117059 ,  0.00999904,  0.04732937,  0.01211804,
         0.01238714,  0.0132119 ,  0.01366057,  0.0089514 ,  0.0140865]),
 3: array([-0.01126357,  0.05261424,  0.05785207,  0.03360043,  0.05927981,
         0.05927981,  0.033355  ,  0.0578699 ,  0.05570744,  0.04727342]),
 4: array([-0.01126357,  0.01003442, -0.00631642,  0.0151311 ,  0.05006177,
         0.05785207,  0.05261424,  0.05927981,  0.05572284,  0.05927981])}

I am looking for a way to iteratively output and name the individual dataframes. I know I can do something like below GS = pd.DataFrame(out_dfs.values()).T[0], to get the first dataframe. Below is the kind of output I am looking for.

0   -0.011264       0   -0.011264
1    0.010034       1   -0.006316
2   -0.006316       2    0.015721
3   -0.006316       3    0.014659
4    0.015893       4    0.050062
5    0.009999       5    0.012762
6    0.057037       6    0.007544
7    0.015499       7    0.055723
8    0.013649       8    0.009963
9    0.044902       9    0.006180
Name: 0, dtype: float64         Name: 1, dtype: float64
1 Answers

I am not sure i completely understood your desired output, but here is my interpretation. Below uses f strings and exec() to EXECute

import pandas as pd

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'

out_dfs = {0: ([-0.01126357,  0.01003442, -0.00631642, -0.00631642,  0.01589343,0.00999904,  0.05703701,  0.01549907,  0.01364896,  0.04490213]),
           1: ([-0.01126357, -0.00631642,  0.01572133,  0.01465883,  0.05006177, 0.0127623 ,  0.00754415,  0.05572284,  0.00996258,  0.00618012,]),
           2: ([-0.01126357,  0.0117059 ,  0.00999904,  0.04732937,  0.01211804,0.01238714,  0.0132119 ,  0.01366057,  0.0089514 ,  0.0140865]),
           3: ([-0.01126357,  0.05261424,  0.05785207,  0.03360043,  0.05927981,0.05927981,  0.033355  ,  0.0578699 ,  0.05570744,  0.04727342]),
           4: ([-0.01126357,  0.01003442, -0.00631642,  0.0151311 ,  0.05006177,0.05785207,  0.05261424,  0.05927981,  0.05572284,  0.05927981])}


for x in out_dfs.keys():
    exec(f"GS{x}=pd.DataFrame(out_dfs.values()).T[{x}]")

print('GS0')
GS0
print('`````````````````````````````````````')
print('GS1')
GS1
print('`````````````````````````````````````')
print('GS2')
GS2

First few outputs:

enter image description here

Related