简体   繁体   中英

What is the nicest (and fastest) way to create a flat dataframe from a multilevel dictionary

I have a dictionary that looks like that:

dic = {'a': {'b': [1,2], 'c': [3,4]}, 'A': {'B': [10,20], 'C': [30, 40]}}

I would like to get a 2 dim dataframe with 3 columns that looks like that:

'a' 'b'  1  
'a' 'b'  2  
'a' 'c'  3  
'a' 'c'  4  
'A' 'B'  10  
'A' 'B'  20  
'A' 'C'  30  
'A' 'C'  40  

IIUC

s=pd.DataFrame(d).stack().explode().reset_index()
  level_0 level_1   0
0       b       a   1
1       b       a   2
2       c       a   3
3       c       a   4
4       B       A  10
5       B       A  20
6       C       A  30
7       C       A  40

Using list comprehension:

import pandas as pd

dic = {'a': {'b': [1,2], 'c': [3,4]}, 'A': {'B': [10,20], 'C': [30, 40]}}

data = [
    (val_1, val_2, val_3)
    for val_1, nest_dic in dic.items()
    for val_2, nest_list in nest_dic.items()
    for val_3 in nest_list
]
df = pd.DataFrame(data)

print(df)
# Output:
#    0  1   2
# 0  a  b   1
# 1  a  b   2
# 2  a  c   3
# 3  a  c   4
# 4  A  B  10
# 5  A  B  20
# 6  A  C  30
# 7  A  C  40

Like this maybe:

In [1845]: pd.concat({k: pd.DataFrame(v).T for k, v in dic.items()},axis=0).reset_index()                                                                                                                   
Out[1845]: 
  level_0 level_1   0   1
0       a       b   1   2
1       a       c   3   4
2       A       B  10  20
3       A       C  30  40

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM