pandas change diagonal

Solutions on MaxInterview for pandas change diagonal by the best coders in the world

showing results for - "pandas change diagonal"
Samuel
16 Jun 2019
1import pandas as pd 
2import numpy as np
3
4df = pd.DataFrame(np.random.randint(1,100, 100).reshape(10, -1))
5
6out = df.where(df.values != np.diag(df),0,df.where(df.values != np.flipud(df).diagonal(0),0,inplace=True))
Deirdre
03 Jun 2018
1In [21]: df.values[[np.arange(df.shape[0])]*2] = 0
2
3In [22]: df
4Out[22]: 
5          0         1         2         3         4
60  0.000000  0.931374  0.604412  0.863842  0.280339
71  0.531528  0.000000  0.641094  0.204686  0.997020
82  0.137725  0.037867  0.000000  0.983432  0.458053
93  0.594542  0.943542  0.826738  0.000000  0.753240
104  0.357736  0.689262  0.014773  0.446046  0.000000
11