Step 1 — Capture the Download URL (One-Time Setup)
-
Open the MOSPI page: https://esankhyiki.mospi.gov.in/
-
Search for your dataset (CPI, WPI, IIP, etc.)
-
Press F12 → Network tab and tick Preserve log
-
Click the Download button on the page
-
In the network log, find the
.xlsx
request (e.g.,cpi_8.xlsx
) and copy the Request URL
Step 1 — Python Automation and Data Processing
# CPI Python Code can be copied directly
url = "https://api.mospi.gov.in/api/download/CPI/cpi_8.xlsx"
output_file = "cpi_8.xlsx"
response = requests.get(url)
response.raise_for_status() # Check for errors
with open(output_file, "wb") as f:
f.write(response.content)
Ind_CPI = pd.read_excel("cpi_8.xlsx")
Ind_CPI['month_end'] = pd.to_datetime(
Ind_CPI['year'].astype(str) + '-' + Ind_CPI['month_code'].replace(0, 12).astype(str) + '-01'
) + pd.offsets.MonthEnd(0)
Ind_CPI = (
Ind_CPI
.groupby(['month_end', 'group'], as_index=False)['index']
.mean()
)
Ind_CPI = Ind_CPI.pivot(index='month_end', columns='group', values='index')
Ind_CPI = Ind_CPI.reset_index()
Ind_CPI['Quarter_End'] = pd.to_datetime(Ind_CPI['month_end']) + pd.offsets.QuarterEnd(0)
Ind_CPI = Ind_CPI.drop(columns=['month_end'])
Ind_CPI_qtr = (
Ind_CPI
.groupby('Quarter_End', as_index=False)
.mean(numeric_only=True) # averages each subgroup's monthly values into quarterly
)
Ind_CPI_qtr_pc = Ind_CPI_qtr.copy()
Ind_CPI_qtr_pc.iloc[:, 1:] = Ind_CPI_qtr_pc.iloc[:, 1:].pct_change(periods=x) # in %
Ind_CPI_qtr_pc = Ind_CPI_qtr_pc.dropna().reset_index(drop=True)
Ind_CPI_qtr_pc['Quarter_End'] = Ind_CPI_qtr_pc['Quarter_End'].dt.date
No comments:
Post a Comment