python - 合并多索引 Pandas 数据帧

标签 python pandas

我有三个多索引 Pandas 数据框 -

df1 = {('parity', np.nan): {('20194', 1990): 0.3333333333333333,
                            ('22204', 1990): 0.0,
                            ('24060', 1990): 0.3333333333333333},
       ('parity', 0.0): {('20194', 1990): 0.0,
                         ('22204', 1990): 0.0,
                         ('24060', 1990): 0.3333333333333333},
       ('parity', 1.0): {('20194', 1990): 0.3333333333333333,
                         ('22204', 1990): 1.0,
                         ('24060', 1990): 0.0},
       ('parity', 2.0): {('20194', 1990): 0.3333333333333333,
                         ('22204', 1990): 0.0,
                         ('24060', 1990): 0.3333333333333333},
       ('education', 0.0): {('20194', 1990): 0.3333333333333333,
                            ('22204', 1990): 0.6666666666666666,
                            ('24060', 1990): 0.6666666666666666},
       ('education', 1.0): {('20194', 1990): 0.6666666666666666,
                            ('22204', 1990): 0.3333333333333333,
                            ('24060', 1990): 0.3333333333333333}}
df1 = pd.DataFrame(data = df1)

enter image description here

df2 = {('parity', 'zip'): {0: '20194', 1: '22204', 2: '24060'},
       ('parity', 'year'): {0: 1990, 1: 1990, 2: 1990},
       ('parity', 'parity'): {0: 1.5, 1: 1.0, 2: 1.0}}
df2 = pd.DataFrame(data = df2)

enter image description here

df3 = {'parity': {('20194', 1990): 1.5, ('22204', 1990): 1.0, ('24060', 1990): 1.0},
       'education': {('20194', 1990): 0.6666666666666666,
                     ('22204', 1990): 0.3333333333333333,
                     ('24060', 1990): 0.3333333333333333}}
df3 = pd.DataFrame(data = df3)

enter image description here

如何合并 zipyear 索引和列上的所有数据框,使它们如下图所示? enter image description here

最佳答案

使用concat索引中具有 zipyear 级别的 MultiIndex 以及列中具有 2 个级别的 MultiIndex:

#convert columns to MultiIndex in index
df2 = df2.set_index([('parity','zip'),('parity','year')])
#created new MultiIndex in columns
df3.columns = pd.MultiIndex.from_product([df3.columns, ['new']])
df = pd.concat([df1, df2, df3],axis=1).rename_axis(['zip','year'])
print (df)
              parity                               education           parity  \
                 NaN       0.0       1.0       2.0       0.0       1.0 parity   
zip   year                                                                      
20194 1990  0.333333  0.000000  0.333333  0.333333  0.333333  0.666667    1.5   
22204 1990  0.000000  0.000000  1.000000  0.000000  0.666667  0.333333    1.0   
24060 1990  0.333333  0.333333  0.000000  0.333333  0.666667  0.333333    1.0   

                education  
            new       new  
zip   year                 
20194 1990  1.5  0.666667  
22204 1990  1.0  0.333333  
24060 1990  1.0  0.333333  

关于python - 合并多索引 Pandas 数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60263064/

相关文章:

python - 在第三个列表中指定的索引处创建一个列表,其中包含另一个列表中的项目

python - 在不同的单元测试中重用生成器

python - 如何将 json 解析为 Pandas 数据框

python - 绘制多个饼图,其位置位于 for 循环中

python - 预测选择/有界兰特

Python XML 文件打开

python - 多个 Postgres 模式的 Django 单元测试失败

python - 如何交换数据集中整列的单元格值?

python-3.x - 使用 Pandas python 从 geonames 导入文本文件

python - 用一个值替换 Pandas 系列中的多个子字符串