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