I have a matrix that was generated as a pivot table. I have included the data below. I need to turn the diagonal into the first column, which effectively re-orients the matrix so that the cell in the diagonal becomes the cell in the first column, for each row.
This is the matrix as rendered in Pandas
This is a representation of what the matrix should look like after.
df = pd.DataFrame({
'exposure':[4500,2000, 2000, 2000, 2000,
6000,10000,3000,2000,1000,
2000,3000,4000,6000],
'due_date':['2019-01-01', '2019-01-01', '2019-01-01', '2019-01-01', '2019-01-01',
'2019-01-02', '2019-01-02', '2019-01-02','2019-01-01','2019-01-04',
'2019-01-03','2019-01-03','2019-01-03','2019-01-04'],
'repaid_date':['2019-01-01', '2019-01-04','2019-01-01', '2019-01-03', '2019-01-02',
'2019-01-03','2019-01-04', '2019-01-02', '2019-01-03', '2019-01-04',
'2019-01-03','2019-01-04','2019-01-03','2019-01-04']})
pivot = df.pivot_table(values='exposure', index='due_date', columns='repaid_date', aggfunc=len)
pivot.fillna(0,inplace=True)
pivot.reset_index(inplace=True)