cmf05
cmf05

Reputation: 411

Building MultiIndex in Pandas DataFrame

I am reading in two files into Python, both with the form:

           0.00902317     0.0270695     0.0451159     0.0631622  \   
0000010  6.962980e-05  7.063750e-05  7.165970e-05  7.269680e-05   
1000010  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00   
2000010  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00 

The first row is an ID number, and the columns are different ages. The two files have different ages comprising them, and only a few common ID#s.

Ultimately I am combining the two dataframes to find the common ID#s. But I want the resulting dataframe

               File 1                      File 2    
           0.00902317     0.0270695     0.0675493     0.1091622  \   
0000010  6.962980e-05  7.063750e-05  0.000000e+00  0.000000e+00   
1000010  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00   
2000010  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00 

Is there a way to make a dataframe that looks like this, multiindexing columns?

Apologies if this is a simple question, I am new to working with dataframes.

Upvotes: 2

Views: 103

Answers (1)

jezrael
jezrael

Reputation: 863541

I think you can use concat:

print (pd.concat([df1, df2], axis=1, keys=['File 1','File 2']))

            File 1                                  File 2            
        0.00902317 0.0270695 0.0451159 0.0631622 0.0675493 0.1091622  
0000010    0.00007  0.000071  0.000072  0.000073       0.0         0.0
1000010    0.00000  0.000000  0.000000  0.000000       0.0         0.0
2000010    0.00000  0.000000  0.000000  0.000000       0.0         0.0

Upvotes: 2

Related Questions