import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(1,100, 100).reshape(10, -1)) out = df.where(df.values != np.diag(df),0,df.where(df.values != np.flipud(df).diagonal(0),0,inplace=True))
Here is what the above code is Doing:
1. Create a DataFrame with random integers
2. Replace the diagonal values with 0
3. Replace the values in the upper triangle with 0
4. Replace the values in the lower triangle with 0