Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How to replace both the diagonals of dataframe with 0 in pandas

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))
Comment

pandas change diagonal

In [21]: df.values[[np.arange(df.shape[0])]*2] = 0

In [22]: df
Out[22]: 
          0         1         2         3         4
0  0.000000  0.931374  0.604412  0.863842  0.280339
1  0.531528  0.000000  0.641094  0.204686  0.997020
2  0.137725  0.037867  0.000000  0.983432  0.458053
3  0.594542  0.943542  0.826738  0.000000  0.753240
4  0.357736  0.689262  0.014773  0.446046  0.000000
Comment

PREVIOUS NEXT
Code Example
Python :: where to import reverse_lazy in django 
Python :: remove n string 
Python :: python lowercase 
Python :: how to roll longitude axis 
Python :: how to make a forever loop in python 
Python :: save pythonpath 
Python :: how to convert input to uppercase in python 
Python :: youtube-dl python download to specific folder 
Python :: python get files in directory 
Python :: activate venv enviroment 
Python :: stringbuilder python 
Python :: python maths max value capped at x 
Python :: python inspect source code 
Python :: python pandas convert comma separated number string to integer list 
Python :: timestamp in python 
Python :: python print without leading whitespace 
Python :: enumerate in python 
Python :: django validator min max value 
Python :: explode dictionary pandas 
Python :: scaling image interpolation python 
Python :: python - make a copy of a df 
Python :: python mock function return value 
Python :: remove blank spaces from a list python 
Python :: django RetrieveUpdateDestroyAPIView 
Python :: pandas groupby size column name 
Python :: discord get user slash command 
Python :: python version kali linux 
Python :: pandas dataframe convert string to float 
Python :: get os information python 
Python :: replace nan with mean 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =