Search code examples
pythonpandasdataframemulti-index

create iteratively multi index and multi columns dataframe in pandas


Let's say that I want to create a multi index and multi column dataframe:

                          X         Y
Planet Continent Country  A    B    C     D 
Earth     Europe England  0.3  0.5  0.6   0.8
          Europe Italy    0.1  0.2  0.4   1.2 
Mars      Tempe  Sirtys   3.2  4.5  2.3   4.2 

I want to do that by iteratively collecting each single row of the dataframe,

row1 =  np.array(['Earth', 'Europe', 'England', 0.3, 0.5, 0.6, 0.8])
row2 =  np.array(['Earth', 'Europe', 'Italy', 0.1, 0.2, 0.4, 1.2])

I know how, starting with rows, I can create a multi-column dataframe, and I know how I can create a multi-index one. But how can I create both? Thanks


Solution

  • row1 =  np.array(['Earth', 'Europe', 'England', 0.3, 0.5, 0.6, 0.8])
    row2 =  np.array(['Earth', 'Europe', 'Italy', 0.1, 0.2, 0.4, 1.2])
    # create a data frame and set index
    df = pd.DataFrame([row1, row2]).set_index([0,1,2])
    # set the index names
    df.index.names = ['Planet', 'Continent', 'Country']
    # create a multi-index and assign to columns
    df.columns = pd.MultiIndex.from_tuples([('X', 'A'), ('X', 'B'), ('Y', 'C'), ('Y', 'D')])
    
                                X         Y     
                                A    B    C    D
    Planet Continent Country                    
    Earth  Europe    England  0.3  0.5  0.6  0.8
                     Italy    0.1  0.2  0.4  1.2