Home > Mobile >  Pandas Multiindex percent change column
Pandas Multiindex percent change column

Time:11-18

I need to calculate a percent change column with respect to the MultiIndex:

import pandas as pd
import numpy as np

row_x1 = ['1','0']
row_x2 = ['1.5','.5']
row_x3 = ['3','1']
row_x4 = ['2','0']
row_x5 = ['3','.5']

index_arrays = [
    np.array(['first', 'first', 'first', 'second', 'second']),
    np.array(['one','two','three','one','two'])
]

df1 = pd.DataFrame(
    [row_x1,row_x2,row_x3,row_x4,row_x5],
    columns=['A'],
    index=index_arrays,
)

print(df1)

Starting with the following data frame:

                A
first  one      1
       two    1.5
       three    3
second one      2
       two      3

The final "percentage change" column should be calculated as shown below:

                A   %
first  one      1   0
       two    1.5  .5
       three    3   1
second one      2   0
       two      3  .5

I have a large data set, and I need to do this programmatically.

CodePudding user response:

Let's do groupby and calculate percent change

df1['A'] = df1['A'].astype(float)
df1['%'] = df1.groupby(level=0)['A'].pct_change().fillna(0)

                A    %
first  one    1.0  0.0
       two    1.5  0.5
       three  3.0  1.0
second one    2.0  0.0
       two    3.0  0.5
  • Related