Fill matrix with transposed version Fill matrix with transposed version numpy numpy

Fill matrix with transposed version


How about (docs):

>>> df.combine_first(df.T)     a    b    c    da  1.0  0.5  0.6  0.5b  0.5  1.0  0.0  0.4c  0.6  0.0  1.0  0.3d  0.5  0.4  0.3  1.0


Here is one alternative way:

>>> m[np.triu_indices_from(m, k=1)] = m.T[np.triu_indices_from(m, k=1)]>>> marray([[ 1. ,  0.5,  0.6,  0.5],       [ 0.5,  1. ,  0. ,  0.4],       [ 0.6,  0. ,  1. ,  0.3],       [ 0.5,  0.4,  0.3,  1. ]])

m[np.triu_indices_from(m, k=1)] returns the values above the diagonal of m and assigns them to the values values above the diagonal of the transpose of m.


With numpy.isnan():

>>> m[np.isnan(m)] = m.T[np.isnan(m)]>>> m     a    b    c    da  1.0  0.5  0.6  0.5b  0.5  1.0  0.0  0.4c  0.6  0.0  1.0  0.3d  0.5  0.4  0.3  1.0

or better, with panda.isnull():

>>> m[pd.isnull(m)] = m.T[pd.isnull(m)]>>> m     a    b    c    da  1.0  0.5  0.6  0.5b  0.5  1.0  0.0  0.4c  0.6  0.0  1.0  0.3d  0.5  0.4  0.3  1.0

which is finally equivalent to @DSM 's solution!