1

My ultimate goal is to compute the pct_change for two rows within a (2, 2) multi-index dataframe but I want the pct_change row to appear below the other two rows. I know about pct_change() method but that doesn't make a new row so instead I'm computing the pct_change as a separate dataframe and appending the pct_change row to an existing dataframe. Here's an example dataframe.

df = pd.DataFrame(data={
  'A': [94128, 28198, -70.04], 'B': [3627, 1483, -59.11]},
                  index=pd.MultiIndex.from_tuples([('Label', '(Jun 1, 2014-Mar 31, 2015)'),
                                                   ('Label', '(Jun 1, 2015-Mar 31, 2016)'),
                                                   ('Label', '(Jun 1, 2015-Mar 31, 2016)')],
                                                  names=['Text', 'Period']))

Looks like this:

                                         A        B
Text  Period                                       
Label (Jun 1, 2014-Mar 31, 2015)  94128.00  3627.00
      (Jun 1, 2015-Mar 31, 2016)  28198.00  1483.00
      (Jun 1, 2015-Mar 31, 2016)    -70.04   -59.11

This is a multi-index and I want to rename the last Period row value to say % Change.

Desired Output:

                                         A        B
Text  Period                                       
Label (Jun 1, 2014-Mar 31, 2015)  94128.00  3627.00
      (Jun 1, 2015-Mar 31, 2016)  28198.00  1483.00
                        % Change    -70.04   -59.11

Is what I'm trying to do even possible given its multi-index complexity?

>>> df.index
MultiIndex(levels=[['Label'], ['(Jun 1, 2014-Mar 31, 2015)', '(Jun 1, 2015-Mar 31, 2016)']],
           labels=[[0, 0, 0], [0, 1, 1]],
           names=['Text', 'Period'])

1 Answer 1

1

I think you can first get_level_values of level Period tolist, remove last value by indexing [:-1] and add new list ['% Change']. Last create new MultiIndex from_tuples:

print df.index.get_level_values('Period')[:-1].tolist() + ['% Change']
['(Jun 1, 2014-Mar 31, 2015)', '(Jun 1, 2015-Mar 31, 2016)', '% Change']

#change multiindex
new_index = zip(df.index.get_level_values('Text'),
                df.index.get_level_values('Period')[:-1].tolist() + ['% Change'])

df.index = pd.MultiIndex.from_tuples(new_index, names = df.index.names)
print df
                                         A        B
Text  Period                                       
Label (Jun 1, 2014-Mar 31, 2015)  94128.00  3627.00
      (Jun 1, 2015-Mar 31, 2016)  28198.00  1483.00
      % Change                      -70.04   -59.11

print df.index
MultiIndex(levels=[[u'Label'], 
           [u'% Change', u'(Jun 1, 2014-Mar 31, 2015)', u'(Jun 1, 2015-Mar 31, 2016)']],
           labels=[[0, 0, 0], [1, 2, 0]],
           names=[u'Text', u'Period'])
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! I had to use list(zip(...)) for python 3) but your approach makes a lot of sense. So take-away is to make an entirely new index containing the % Change value and then swapping out the current multi-index with the new_index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.