Given a sample data which could be downloaded from
Let's say the MoM
columns values in 2022-04-30
are monthly changes predicted with unit percentage (%).
I will need to calculate year over year changes based on MoM and value columns for import and export separately.
I have achieved that with code below, but how could convert it to a function to make it easier to implement for multiple columns (let's if I have other columns: GDP_MoM, GDP_value, CPI_MoM, CPI_value, etc. in df
)? Thanks for your help at advance.
import pandas as pd
df = pd.read_excel('./sample_data1.xlsx')
df.tail(5)
df['import_value_YoY_based_MoM'] = ((df['import_MoM']/100 1)*df['import_value'].shift(1) / df['import_value'].shift(12))-1
df['export_value_YoY_based_MoM'] = ((df['export_MoM']/100 1)*df['export_value'].shift(1) / df['export_value'].shift(12))-1
df['import_value_YoY'] = df['import_value'] / df['import_value'].shift(12) -1
df['export_value_YoY'] = df['export_value'] / df['export_value'].shift(12) -1
print(df.tail(5))
Out:
CodePudding user response:
Use custom funtion with f-string
s with input for both columns names in pairs in list cols
:
def f(df, col1, col2):
df[f'{col2}_YoY_based_{col1}'] = ((df[col1]/100 1)*df[col2].shift(1) / df[col2].shift(12))-1
df[f'{col2}_YoY'] = df[col2] / df[col2].shift(12) -1
return df
cols = [('import_MoM','import_value'), ('export_MoM','export_value')]
for col1, col2 in cols:
df = f(df, col1, col2)