Set values on the diagonal of pandas.DataFrame

后端 未结 5 2223
日久生厌
日久生厌 2020-11-27 17:56

I have a pandas dataframe I would like to se the diagonal to 0

import numpy
import pandas

df = pandas.DataFrame(numpy.random.rand(5,5))
df

Out[6]:
     0           


        
5条回答
  •  忘掉有多难
    2020-11-27 18:35

    Using np.fill_diagonal(df.values, 1) Is the easiest, but you need to make sure your columns all have the same data type I had a mixture of np.float64 and python floats and it would only effect the numpy values. to fix you have to cast everything to numpy.

提交回复
热议问题