0

I have a database that includes monthly time series data on around 15 different indicators. The data is all in the same format, year-to-date values and year-to-date growth. January data is missing, with data for each indicator starting with the year-to-date total as of February.

For each indicator I want to turn the year-to-date data into monthly values. The code below does that.

But I want to be able to run this as a loop over all the 15 indictators, and then automatically rename each dataframe that results to include a reference to the category it belongs to. For example, one category of data is sales in value terms, so when I apply the code to that category, I want the output of df_m to be renamed as sales_m, and df_yoy as sales_yoy.

I thought I could so this by defining a list of the 15 indicators to start with, and then somehow assigning that list to the dataframes produced by the loop. But I can't make that work.

 category = ['sales', 'construction']

 df_m = df.loc[:, df.columns.str.contains('Monthly')]
 df_ytd = df.drop(df.filter(regex='Monthly').columns, axis=1)
 df_ytd = df_ytd.fillna(method='bfill', limit=1)
 df_ytd.loc[df_ytd.index.month.isin([1,2]), :] = df_ytd / 2
 df_ytd.columns = df_ytd.columns.str.replace(', YTD', '')
 df_m.columns = df_m.columns.str.replace('YTD, ', '').str.replace(', Monthly', '')
 df_m = df_m.fillna(df_ytd)
 df_yoy = df_m.pct_change(periods=12) * 100

sales_m = df_m
    
paul
  • 89
  • 1
  • 9
  • I'm not entirely sure I understand what you are asking, but this could be of interest: https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables – Ture Pålsson Sep 11 '21 at 07:20
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 17 '21 at 08:38

0 Answers0