Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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 :: CMake Error at pybind11/tools/FindPythonLibsNew.cmake:131 (message): Python config failure: 
Python :: python cv2 unblur 
Python :: blender python get current filename 
Python :: pandas append new column 
Python :: You will be provided a file path for input I, a file path for output O, a string S, and a string T. 
Python :: minio python remove a bucket 
Python :: scikit learn decistion tree 
Python :: install python 3 
Python :: python get colorscale 
Python :: munshi premchand 
Python :: inverse box-cox transformation python 
Python :: how to get last element of list in python 
Python :: python trace code execution 
Python :: __add__ 
Python :: WSGIPassAuthorization on 
Python :: change index function for class python 
Python :: check even or odd in single line 
Python :: python create zip file 
Python :: reduce dataframe merge 
Python :: are there learning activities for django-debug-toolbar 
Python :: Binary search tree deleting 
Python :: Sorting a list using a named function 
Python :: random list 
Python :: append numeric number in and auto increment in using pandas 
Python :: how do i get auth user model dynamically in django? 
Python :: python random number between 0 and 1 
Python :: set lable of field django 
Python :: length of dictionary python 
Python :: reverse relationship in django for one to one field for usage in Django rest serializer 
Python :: pure imagination 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =