如何合并两个具有不同索引级别的数据框?

时间:2019-01-05 14:23:26

标签: python-3.x

我的第一个DataFrame代码:

 data = {'Sr.No.': Sr_no,
    'CompanyNames': Company_Names,
    'YourChoice1': ['1','2','3','4','5','6','7','8','9'],
    'YourChoice2': ['1','2','3','4','5','6','7','8','9']}


 df = pd.DataFrame(data, columns = ['Sr.No.', 'CompanyNames','YourChoice1','YourChoice2'])

.csv文件中此数据帧的输出为:

Output of 1st DataFrame

第二个DataFrame代码:

 data = {'BB': {'Field1': {'Previous Data': ['1','2','3','4','5','6','7','8','9'],
                      'Current Data': ['1','2','3','4','5','6','7','8','9']},

           'Field2': {'Previous Data': ['1','2','3','4','5','6','7','8','9'],
                      'Current Data': ['1','2','3','4','5','6','7','8','9']},

           'Field3': {'Previous Data': ['1','2','3','4','5','6','7','8','9'],
                      'Current Data': ['1','2','3','4','5','6','7','8','9']}
           },


    'MD': {'Field1': {'Previous Data': ['1','2','3','4','5','6','7','8','9'],
                      'Current Data': ['1','2','3','4','5','6','7','8','9']},

           'Field2': {'Previous Data': ['1','2','3','4','5','6','7','8','9'],
                      'Current Data': ['1','2','3','4','5','6','7','8','9']}
           }
    }

 form = {(outerKey,midKey,innerKey): values   for outerKey, midDict in data.items()   for midKey, innerDict in midDict.items()  for innerKey, values in innerDict.items()}

 df.to_csv('CSV_File_Name.csv', index=False)

此数据帧的输出为:

Output of 2nd DataFrame

我想组合这些数据帧,以便获得以下期望的输出:

Desired Output

0 个答案:

没有答案