The code below creates a bunch of data tables form the dictionary below. I am trying to add Values as the index parameter for all the data tables but they all the table values turn into nan. How will I be able to change it so that the Indexes 0 to 2 are replaced with First, Second, Third.
Dictionary:
Outcomes = {
'Values':{
'First': {
'option 1': np.array([12,345,5412]),
'option 2': np.array([2315,32,1]),
'option 3': {'row 1': np.array([232,3,1]),
'row 2': np.array([3,4,5]),
'row 3': np.array([15,6,12])}
}
}
}
Code:
import pandas as pd
import numpy as np
Values = ['First', 'Second', 'Third']
def get_nested_df(dic, concat_key="", df_dic=dict()):
rows = {k:v for k,v in dic.items() if not isinstance(v, dict)}
if rows:
df_dic.update({concat_key: pd.DataFrame.from_dict(rows)})
for k,v in dic.items():
if isinstance(v, dict):
get_nested_df(v, f"{concat_key} {k}", df_dic)
return df_dic
df_dic = get_nested_df(Outcomes)
for k,v in df_dic.items():
#print(f"{k}\n{v}\n")
display(pd.DataFrame(v, index=Values).style.set_caption(k).set_table_styles([{
'selector': 'caption',
'props': [
('color', 'red'),
('font-size', '16px'),
('text-align', 'center')
]
}]))
Output:
