Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

python - Nested Dictionary to MultiIndex pandas DataFrame (3 level)

I would like to do the equivalent of this for a 3 level nested dictionary

Nested dictionary to multiindex dataframe where dictionary keys are column labels

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using an example of three level dict

In [1]: import pandas as pd

In [2]: dictionary = {'A': {'a': {1: [2,3,4,5,6],
   ...:                           2: [2,3,4,5,6]},
   ...:                     'b': {1: [2,3,4,5,6],
   ...:                           2: [2,3,4,5,6]}},
   ...:               'B': {'a': {1: [2,3,4,5,6],
   ...:                           2: [2,3,4,5,6]},
   ...:                     'b': {1: [2,3,4,5,6],
   ...:                           2: [2,3,4,5,6]}}}

And the following dictionary comprehension based on the one from the question you linked

In [3]: reform = {(level1_key, level2_key, level3_key): values
   ...:           for level1_key, level2_dict in dictionary.items()
   ...:           for level2_key, level3_dict in level2_dict.items()
   ...:           for level3_key, values      in level3_dict.items()}

Which gives

In [4]: reform
Out[4]:
{('A', 'a', 1): [2, 3, 4, 5, 6],
 ('A', 'a', 2): [2, 3, 4, 5, 6],
 ('A', 'b', 1): [2, 3, 4, 5, 6],
 ('A', 'b', 2): [2, 3, 4, 5, 6],
 ('B', 'a', 1): [2, 3, 4, 5, 6],
 ('B', 'a', 2): [2, 3, 4, 5, 6],
 ('B', 'b', 1): [2, 3, 4, 5, 6],
 ('B', 'b', 2): [2, 3, 4, 5, 6]}

For pandas DataFrame

In [5]: pd.DataFrame(reform)
Out[5]:
   A           B
   a     b     a     b
   1  2  1  2  1  2  1  2
0  2  2  2  2  2  2  2  2
1  3  3  3  3  3  3  3  3
2  4  4  4  4  4  4  4  4
3  5  5  5  5  5  5  5  5
4  6  6  6  6  6  6  6  6

In [6]: df = pd.DataFrame(reform).T
Out[6]:
       0  1  2  3  4
A a 1  2  3  4  5  6
    2  2  3  4  5  6
  b 1  2  3  4  5  6
    2  2  3  4  5  6
B a 1  2  3  4  5  6
    2  2  3  4  5  6
  b 1  2  3  4  5  6
    2  2  3  4  5  6

As you can see, you could increase the number of levels easily by adding another line to the comprehension and new key to tuple.

Bonus: add names to the indexes

In [7]: names=['level1', 'level2', 'level3']

In [8]: df.index.set_names(names, inplace=True)

In [9]: df
Out[9]:
                      0  1  2  3  4
level1 level2 level3
A      a      1       2  3  4  5  6
              2       2  3  4  5  6
       b      1       2  3  4  5  6
              2       2  3  4  5  6
B      a      1       2  3  4  5  6
              2       2  3  4  5  6
       b      1       2  3  4  5  6
              2       2  3  4  5  6

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...