python - 如何将字典转换为多级数据框?

标签 python python-3.x pandas dictionary dataframe

给定:

A = pd.DataFrame([[1, 5, 2, 8, 2], [2, 4, 4, 20, 2], [3, 3, 1, 20, 2], [4, 2, 2, 1, 0], 
              [5, 1, 4, -5, -4], [1, 5, 2, 2, -20], [2, 4, 4, 3, 0], [3, 3, 1, -1, -1], 
              [4, 2, 2, 0, 0], [5, 1, 4, 20, -2]],
             columns=['a', 'b', 'c', 'd', 'e'],
             index=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

B = pd.DataFrame([[0, 0, 0, 8, 2], [1, 1, 1, 1, 1], [0, 0, 0, 8, 2], [0, 0, 2, 1, 0], 
              [5, 1, 4, -5, -4], [0, 0, 0, 8, 2], [2, 4, 4, 3, 0], [1, 3, 1, -1, -1], 
              [1, 1, 2, 0, 0], [2, 2, 2, 20, -2]],
             columns=['a', 'b', 'c', 'd', 'e'],
             index=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

test_list = [('test1', A), ('test2', B)]
d_test = dict(test_list)

然后,当我尝试将此字典转换为多级数据框时遇到麻烦。但我不知道如何指定多级,我希望“键”成为第二级数据框的名称。这是期望的输出

df

    test_1              test_2
    a   b   c   d   e   a   b   c   d   e
1   1   5   2   8   2   0   0   0   8   2
2   2   4   4   20  2   1   1   1   1   1
3   3   3   1   20  2   0   0   0   8   2
4   4   2   2   1   0   0   0   2   1   0
5   5   1   4   -5  -4  5   1   4   -5  -4
6   1   5   2   2   -20 0   0   0   8   2
7   2   4   4   3   0   2   4   4   3   0
8   3   3   1   -1  -1  1   3   1   -1  -1
9   4   2   2   0   0   1   1   2   0   0
10  5   1   4   20  -2  2   2   2   20  -2

最佳答案

只是为了在这里得到一个答案 (已经在评论中给出)这里(再次):

import pandas as pd
A = pd.DataFrame([[1, 5, 2, 8, 2], [2, 4, 4, 20, 2], [3, 3, 1, 20, 2], [4, 2, 2, 1, 0], 
              [5, 1, 4, -5, -4], [1, 5, 2, 2, -20], [2, 4, 4, 3, 0], [3, 3, 1, -1, -1], 
              [4, 2, 2, 0, 0], [5, 1, 4, 20, -2]],
             columns=['a', 'b', 'c', 'd', 'e'],
             index=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
B = pd.DataFrame([[0, 0, 0, 8, 2], [1, 1, 1, 1, 1], [0, 0, 0, 8, 2], [0, 0, 2, 1, 0], 
              [5, 1, 4, -5, -4], [0, 0, 0, 8, 2], [2, 4, 4, 3, 0], [1, 3, 1, -1, -1], 
              [1, 1, 2, 0, 0], [2, 2, 2, 20, -2]],
             columns=['a', 'b', 'c', 'd', 'e'],
             index=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
AB=pd.concat([A,B], axis=1)
header = ['test1','test1','test1','test1','test1','test2','test2','test2','test2','test2']
AB.columns = pd.MultiIndex.from_tuples(list(zip(header, AB.columns)))
print(AB) # gives what was asked for 
print("test1: \n", AB.test1) # gives A
print("test2: \n", AB.test2) # gives B

实现相同目标的另一种(实际情况)方式(上述评论中给出的答案):

test_list = [('test1', A), ('test2', B)]
d_test = dict(test_list)
AB = pd.concat(d_test.values(), keys=d_test.keys(), axis=1) 
# what means: AB = pd.concat([A,B], keys=['test1', 'test2'], axis=1) 

上面的代码输出:

   test1               test2             
       a  b  c   d   e     a  b  c   d  e
1      1  5  2   8   2     0  0  0   8  2
2      2  4  4  20   2     1  1  1   1  1
3      3  3  1  20   2     0  0  0   8  2
4      4  2  2   1   0     0  0  2   1  0
5      5  1  4  -5  -4     5  1  4  -5 -4
6      1  5  2   2 -20     0  0  0   8  2
7      2  4  4   3   0     2  4  4   3  0
8      3  3  1  -1  -1     1  3  1  -1 -1
9      4  2  2   0   0     1  1  2   0  0
10     5  1  4  20  -2     2  2  2  20 -2
test1: 
     a  b  c   d   e
1   1  5  2   8   2
2   2  4  4  20   2
3   3  3  1  20   2
4   4  2  2   1   0
5   5  1  4  -5  -4
6   1  5  2   2 -20
7   2  4  4   3   0
8   3  3  1  -1  -1
9   4  2  2   0   0
10  5  1  4  20  -2
test2: 
     a  b  c   d  e
1   0  0  0   8  2
2   1  1  1   1  1
3   0  0  0   8  2
4   0  0  2   1  0
5   5  1  4  -5 -4
6   0  0  0   8  2
7   2  4  4   3  0
8   1  3  1  -1 -1
9   1  1  2   0  0
10  2  2  2  20 -2

关于python - 如何将字典转换为多级数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43160278/

相关文章:

python - VSCode 调试器自动附加到子进程

python - 带有 try/except 的函数不返回!在 python 3

Python:如何流式传输音频文件(m4a/webm)而不是简单地下载它?

python - 计算单词中的音节数

python - 将一个模块调用到另一个模块

python - 主要用于静态站点的轻量级 Web 框架

python-3.x - Google Cloud Speech to Text 中的 enable_speaker_diarization 标记出错

python - 将多个 if/else 语句应用于 pandas 中的 groupby 对象

python - 重置 Pandas 时间戳的时间部分

python - 在多索引数据框中填充缺失的时间值