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

how to change the main diagonal in pandas

import numpy as np

s = pd.Series(data=[1,2,3],index=['a','b','c'])

df = pd.DataFrame(0, index=s.index, columns=s.index, dtype=s.dtype)

np.fill_diagonal(df.values, s)#you can also make s to be just a single float
print (df)
   a  b  c
a  1  0  0
b  0  2  0
c  0  0  3
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 :: pathlib change extension 
Python :: how to move an item from one list to another python 
Python :: selenium check if driver is open python 
Python :: how to swap numbers in python mathematically 
Python :: how to unstack multiindex pandas 
Python :: socket for api in django 
Python :: code optimization in python 
Python :: min() and max() Function in python 
Python :: python left string 
Python :: what mean import in python 
Python :: python iterate over instances of class 
Python :: make guessing game by python 
Python :: data.head on terminal 
Python :: think python 
Python :: count variable in class python 
Python :: forward checking algorithm python 
Python :: elif "wikipedia" 
Python :: make row readonly tablewidget pyqt 
Python :: change group box title font size 
Python :: python prime number 
Python :: split a column into two columns pandas 
Shell :: lumen run command 
Shell :: how to kill apache process in linux 
Shell :: list process using port 
Shell :: ubuntu restart mariadb 
Shell :: check chrome version ubuntu via terminal 
Shell :: update composer ubuntu 
Shell :: dns flush command 
Shell :: how to convert ui to py pyqt5 
Shell :: yarn install global 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =