简体   繁体   中英

Python Pandas Multi-index: Rename Specific Index Row Value

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'])

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM