I have a large dictionary with the following simplistic structure:
dict_1 = {'A': {'x1': 0, 'x2': 1}, 'B': {'x1': 0, 'x2': 1, 'x3': 0}, 'C': {'x1': 1, 'x3': 1}}
I would like to transform it to a pandas dataframe with the following structure
"Var_1" "Var_2"
A x_1 0
A x_2 1
B x_1 0
B x_2 1
B x_3 0
C x_1 1
C x_3 1
My first attempt was to do something like
dict_1 = pd.DataFrame(dict_1)
dict_1 = dict_1.unstack().reset_index(level=1).set_axis(["Var_1","Var_2"], axis=1)
dict_1.dropna(inplace=True)
However, I realized that during the whole process, many np.NaN are involved in all transformations over-saturating the memory and therefore, making the whole transformation a lot of time- and resource-consuming.
Is there a simpler and faster way to achieve this?