I have a dictionary:
data1 = {
'AI': [3, {'G': 0.0, 'C': 0.31666666666666665, 'L': 0.3333333333333333, 'U': 0.0, 'J': 0.0, 'W': 0.0, 'O1': 0.3333333333333333, 'O2': 0.016666666666666666, 'T1': 0.0, 'T2': 0.0, 'N': 0.0, 'B': 0.0, 'R': 0.0, 'D': 0.0, 'M': 0.0}, [0.3333333333333333, 0.3333333333333333, 0.3333333333333333]],
'CY': [96, {'G': 0.010416666666666666, 'C': 0.0, 'L': 0.46875, 'U': 0.0, 'J': 0.010416666666666666, 'W': 0.0, 'O1': 0.41666666666666663, 'O2': 0.020833333333333332, 'T1': 0.0, 'T2': 0.07291666666666666, 'N': 0.0, 'B': 0.0, 'R': 0.0, 'D': 0.0, 'M': 0.0}, [0.3333333333333333, 0.3333333333333333, 0.3333333333333333]],
'PY': [127, {'G': 0.0, 'C': 0.0, 'L': 0.42276422764227645, 'U': 0.008130081300813009, 'J': 0.0, 'W': 0.0, 'O1': 0.4552845528455285, 'O2': 0.04065040650406505, 'T1': 0.0, 'T2': 0.04878048780487805, 'N': 0.020325203252032523, 'B': 0.0040650406504065045, 'R': 0.0, 'D': 0.0, 'M': 0.0}, [0.3333333333333333, 0.3333333333333333, 0.3333333333333333]],
'SR': [1, {'G': 0.0, 'C': 0.0, 'L': 0.0, 'U': 0.0, 'J': 0.0, 'W': 0.0, 'O1': 0.0, 'O2': 1.0, 'T1': 0.0, 'T2': 0.0, 'N': 0.0, 'B': 0.0, 'R': 0.0, 'D': 0.0, 'M': 0.0}, [0.33333333333333337, 0.33333333333333337, 0.33333333333333337]],
'CV': [119, {'G': 0.03022827836191888, 'C': 0.09570154943733565, 'L': 0.1787022898077257, 'U': 0.059038954704106396, 'J': 0.01667767082036904, 'W': 0.0008338835410184519, 'O1': 0.46697478297033307, 'O2': 0.03193607185392467, 'T1': 0.0, 'T2': 0.08238175915743774, 'N': 0.0208470885254613, 'B': 0.00833883541018452, 'R': 0.0, 'D': 0.00833883541018452, 'M': 0.0}, [0.33216690905268464, 0.3354010574206815, 0.3324320335266338]],
'BS': [65, {'G': 0.024576271186440683, 'C': 0.1033898305084746, 'L': 0.08576271186440679, 'U': 0.37796610169491535, 'J': 0.016949152542372885, 'W': 0.0, 'O1': 0.21864406779661022, 'O2': 0.07271186440677968, 'T1': 0.050847457627118654, 'T2': 0.013559322033898308, 'N': 0.016949152542372885, 'B': 0.0016949152542372885, 'R': 0.0, 'D': 0.016949152542372885, 'M': 0.0}, [0.3346524499339513, 0.3346524499339513, 0.33069510013209746]],
'RD': [24, {'G': 0.041666666666666664, 'C': 0.0, 'L': 0.29166666666666663, 'U': 0.0, 'J': 0.0, 'W': 0.0, 'O1': 0.5833333333333333, 'O2': 0.041666666666666664, 'T1': 0.0, 'T2': 0.0, 'N': 0.041666666666666664, 'B': 0.0, 'R': 0.0, 'D': 0.0, 'M': 0.0}, [0.3333333333333333, 0.3333333333333333, 0.3333333333333333]]
}
The form is:
d1 = {
A1: [n1, {B1: m1, B2: m2, ...}, [p1, q1, r1]],
...
}
I want to create a dataframe that contains the top level keys (A’s) of the dictionary as rows, and the inner level keys (B’s) as the columns. The B keys do not vary from entry to entry. The values m should be the cell values.
What is the most pythonic way to accomplish this?
I have read Create a pandas dataframe from a dictionary of dictionaries, but the dictionary here is nested inside a list, while that is a simple dictionary of dictionaries. Not comparable.
>Solution :
You can use pd.DataFrame.from_dict with orient='index':
out = pd.DataFrame.from_dict({k: v[1] for k, v in data1.items()}, orient='index')
Output:
G C L U J W O1 \
AI 0.000000 0.316667 0.333333 0.000000 0.000000 0.000000 0.333333
CY 0.010417 0.000000 0.468750 0.000000 0.010417 0.000000 0.416667
PY 0.000000 0.000000 0.422764 0.008130 0.000000 0.000000 0.455285
SR 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
CV 0.030228 0.095702 0.178702 0.059039 0.016678 0.000834 0.466975
BS 0.024576 0.103390 0.085763 0.377966 0.016949 0.000000 0.218644
RD 0.041667 0.000000 0.291667 0.000000 0.000000 0.000000 0.583333
O2 T1 T2 N B R D M
AI 0.016667 0.000000 0.000000 0.000000 0.000000 0.0 0.000000 0.0
CY 0.020833 0.000000 0.072917 0.000000 0.000000 0.0 0.000000 0.0
PY 0.040650 0.000000 0.048780 0.020325 0.004065 0.0 0.000000 0.0
SR 1.000000 0.000000 0.000000 0.000000 0.000000 0.0 0.000000 0.0
CV 0.031936 0.000000 0.082382 0.020847 0.008339 0.0 0.008339 0.0
BS 0.072712 0.050847 0.013559 0.016949 0.001695 0.0 0.016949 0.0
RD 0.041667 0.000000 0.000000 0.041667 0.000000 0.0 0.000000 0.0