I have a DataFrame that looks like the one on the left below and I need to re-arrange so it looks like the one on the right:
  XYZ  year  month  values                     A      B      C      D
0   A  2006      8   35601            2005-01  NaN    NaN    NaN    NaN
1   A  2005      3   13164            2005-02  36889  NaN    NaN    NaN
2   A  2006      5   45830            2005-03  13164  NaN    NaN    NaN
3   A  2005      5   47495            2005-04  NaN    NaN    NaN    NaN
4   A  2005      2   36889     ->     .        
5   B  2006      1   33694            2006-01  NaN    33694  NaN    NaN
6   C  2006      2    7400            2006-02  NaN    NaN    7400   NaN
7   C  2006      6    5698            .
8   D  2006      6   14049            .
9   A  2005      9   22650            2006-08  35601  NaN    NaN    NaN
I have created the values that will be used as columns in the new DF, something like:
df1 = pd.DataFrame(..., columns=df.XYZ.unique().tolist())
and I have created the values that will be used as index in the new DF, something like:
df1 = pd.DataFrame(..., index=pd.date_range('2005-01-01', periods=24,freq='M').to_period('m'))
I need help to understand how to iterate through df so that I can use the matching value for each column, based that the new index is mapped correctly to legacy DF values for ['year','month']
Any help is appreciated, thanks!
 
    