Pivot reshape dataframe
my intial dataframe :

     id  p1_201501  p1_201502  p1_201803  p2_201801  p2_201812
0   x12        125         12          6        500         14
1  nz15        250         16        600        423         32
2  qz15        350          4         20        223         32


my program:

import pandas as pd

df = pd.melt(df, id_vars=["id"], var_name="product", value_name="values").reset_index(drop=True)
df['date'] = df['product'].str.split('_',n = 1, expand=True)[1]
df['year'] = df['date'].astype(str).str[:-2]
df['month'] = df['date'].astype(str).str[4:]

#drop unused columns
df.drop(['product','date'],axis=1,inplace=True)

df = df[['id', 'year','month','values']]
df = df.sort_values(['id', 'year','month'], ascending=[True,True,True]).reset_index(drop=True)
df.set_index(['id','year','month'])

#just sum each annual total sales for each id
df = df.groupby(['id','year'])['values'].sum()

print(df)


final result:

id    year
nz15  2015     266
      2018    1055
qz15  2015     354
      2018     275
x12   2015     137
      2018     520