pierre_j
pierre_j

Reputation: 983

How to initialize a multiindex, so that we can append values?

I am initializing a multi-index at the beginning of my code. Different functions contributes to filling it.

One of them (in another place of the code) initializes it from scratch. To do this, I am basically creating a new DataFrame, but re-using the shape of the one I have initialized at the beginning of my code.

So code is not working well, it looks like this:

# Beginning of the code, initialization of `summary`.
c_array = [['Index', 'Index'],['First', 'Last']]
cmidx = pd.MultiIndex.from_arrays(c_array)
rmidx = pd.MultiIndex(levels=[[],[]],
                      codes=[[],[]],
                      names=['CDE','Period'])

summary = pd.DataFrame(index=rmidx, columns=cmidx)


# Function that re-initializes `summary`,
# keeping its original shape, columns name, and so on...

# New labels
index_label = [('a','b'),('c','d')]
# New values
values = [[20,30],[40,50]]

# New `summary`
summary = pd.DataFrame(values,
                       index = summary.index.reindex(index_labels),
                       columns = summary.columns)

summary

                   Index     
                   First Last
  ((a, b), (c, d))    20   30
  (-1, -1)            40   50

I was expecting to keep the names, and have (c, d) as labels for the second row, i.e. :

               Index     
   CDE Period  First Last
     a      b     20   30
     c      d     40   50

Please, does someone know what mistake I am doing? I thank you for your help, Bests!

EDIT

There seems to be something wrong with the way I first initialize the multi-index.

One way that works (but it forces me to re-create a multi-index, and have the names for the levels)

rmidx = pd.MultiIndex.from_tuples(index_label, names=['CDE', 'Period'])
summary = pd.DataFrame(values, index = rmidx,
                       columns = summary.columns)

>>>             Index     
           First Last
CDE Period           
a   b         20   30
c   d         40   50

In this case,

summary.index
>>> MultiIndex([('a', 'b'),
                ('c', 'd')],
               names=['CDE', 'Period'])

Thanks for any help!

Upvotes: 0

Views: 786

Answers (1)

lucky6qi
lucky6qi

Reputation: 994

import pandas as pd
# Beginning of the code, initialization of `summary`.
c_array = [['Index', 'Index'],['First', 'Last']]
cmidx = pd.MultiIndex.from_arrays(c_array)
rmidx = pd.MultiIndex(levels=[[],[]],
                      codes=[[],[]],
                      names=['CDE','Period'])

summary = pd.DataFrame(index=rmidx, columns=cmidx)


# # Function that re-initializes `summary`,
# # keeping its original shape, columns name, and so on...

# New labels
index_label = [['a','b'],['c','d']]
# New values
values = [[20,30],[40,50]]

# New `summary`
summary = pd.DataFrame(values,
                       index = index_label,
                       columns = summary.columns)
summary.index.names = ['CDE','Period']

Upvotes: 2

Related Questions