Dual nested dictionary to stacked DataFrame

Try with explode after 0.25 pandas

pd.DataFrame(subnetwork_dct).stack().explode().reset_index()

Comprehension

pd.DataFrame([
    (k0, k1, v) for k0, d in subnetwork_dct.items()
                for k1, V in d.items()
                for v     in V
])

            0  1           2
0   518418568  2   478793912
1   518418568  2   518418568
2   518418568  2   518758448
3   518418568  3   478793912
4   518418568  3   518418568
5   518418568  3   518758448
6   518418568  3  1037590624
7   518418568  4   478793912
8   518418568  4   518418568
9   518418568  4   518758448
10  518418568  4  1037590624
11  552214776  2   431042800
12  552214776  2   552214776
13  552214776  3   431042800
14  993280096  2   456917000
15  993280096  2   993280096
16  993280096  3   456917000
17  993280096  3   993280096
18  993280096  4   456917000
19  993280096  4   993280096

Not sure if pretty, but it's concise... kind of.

df_stacked = (pd.DataFrame(subnetwork_dct).T
                      .stack()
                      .apply(pd.Series)
                      .stack()
                      .reset_index(-1, drop=True)
                      .reset_index())
df_stacked.columns = [0,1,2]


Out[76]: 
           0  1            2
0  518418568  2  478793912.0
1  518418568  2  518418568.0
2  518418568  2  518758448.0
3  518418568  3  478793912.0
4  518418568  3  518418568.0

Tags:

Python

Pandas