I have a dataframe:
df = pd.DataFrame({
'BU': ['Total', 'Total', 'Total', 'ABC', 'ABC', 'ABC'],
'Line_Item': ['Revenues','EBT', 'Expenses', 'Revenues', 'EBT', 'Expenses'],
'1Q16': [100, 120, 200, 200, 190, 210],
'2Q16': [120, 120, 100, 200, 190, 210],
'3Q16': [200, 250, 450, 120, 180, 190],
'4Q16': [150, 850, 790, 410, 170, 990],
'1Q17': [120, 240, 790, 10, 170, 990],
'2Q17': [110, 140, 650, 50, 100, 120]})
I want new columns with the rolling difference between the columns. So I want the annual differences, which is, say, the difference in revenues in 1Q16 and 1Q17. I also want the differences between quarters. The period between the columns should a be parameter.
I know how to take the percent change by appling the pct_change function.
df_qtrs_YoY = df[['1Q16', '2Q16', '3Q16', '4Q16', '1Q17', '2Q17']].pct_change(periods=4, axis=1).dropna(1)
Could I also do so with difference? My dataset is large. So a vectorized solution might work best. The output should look like:
df1 = pd.DataFrame({
'BU': ['Total', 'Total', 'Total', 'ABC', 'ABC', 'ABC'],
'Line_Item': ['Revenues','EBT', 'Expenses', 'Revenues', 'EBT', 'Expenses'],
'1Q17': [20, 140, 590, -190, -20, 0],
'2Q17': [-10, 20, -30, -150, -90, -90]})