Home > Software design >  Re-arrange/combine dataframes in dictionaries
Re-arrange/combine dataframes in dictionaries

Time:12-21

I have a dictionary which is made up of two data frames

dict_keys(['on_folds', 'global'])

each data frame is made up of two series

results['global'].keys() = Index(['rMSE', 'Bias'])

Each series has indexes with floats

results['global']['rMSE'].keys() = Index(['lasso_reg', 'lasso_class']),

I would like to build a new dictionary by combining the items of the old ones into the data frames

dict_keys(['lasso_reg', 'lass_class'])

with series

results['lasso_reg'].keys() = Index(['rMSE', 'Bias'])

and floats

results['lasso_reg']['rMSE'].keys() = Index(['on_folds', 'global'])

This is essentially a switch of the indexes. I am not sure if transposing or combing would do it. How do I do that?

Update: here example of the contents of the dictionary

I have the following dictionary

{'on_folds':     rMSE    95% CI      Bias        SD
 lasso_reg    1.690569  0.722917  3.257023  1.280235
 lasso_class  2.616418  0.662500  4.025593  1.863953
 xgb_class    6.899894  0.409375  9.261215  2.760382
 xgb_reg      5.436057  0.482292  8.004550  2.138652
 rf_reg       4.998613  0.659375  7.028649  2.291934
 rf_class     3.820162  0.659375  6.436152  1.651670

and I would like to change it so it looks like this

{'lasso_reg':       rMSE    95% CI      Bias        SD
 on_folds       1.690569  0.722917  3.257023  1.280235
'lasso_class':      rMSE    95% CI      Bias        SD
 on_folds       1.690569  0.722917  3.257023  1.280235
etc.

CodePudding user response:

I'm still not sure what your data looks like but try this one:

out = {}
for k, df in dct.items():
    temp = df.loc[['lasso_reg','lass_class'],:]
    temp.index = [k, k]
    out.setdefault('lasso_reg', []).append(temp.iloc[0])
    out.setdefault('lass_class', []).append(temp.iloc[1])
    
out = {k: pd.concat(v) for k,v in out.items()}
  • Related