Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #change #main #diagonal #pandas
ADD COMMENT
Topic
Name
9+6 =