Учитывая, что последний столбец отсортирован соответствующим образом, мы могли бы использовать следующий код:
import pandas as pd
import numpy as np
# define data frame
data = pd.DataFrame({
'name1': ['A', 'A', 'B'],
'name2': ['B', 'C', 'C'],
'correlation': [0.1, 0.2, 0.3]})
# get correlation column and dimension
correlation = data['correlation'].values
dimension = correlation.shape[0]
# define empty matrix to fill and unit matrix
matrix_upper_triangular = np.zeros((dimension, dimension))
# fill upper triangular matrix with one half at diagonal
counter = 0
for (row, column), element in np.ndenumerate(matrix_upper_triangular):
# half of diagonal terms
if row == column:
matrix_upper_triangular[row, column] = 0.5
# upper triangular values
elif row < column:
matrix_upper_triangular[row, column] = correlation[counter]
counter = counter + 1
else:
pass
# add upper triangular + lower triangular matrix
correlation_matrix = matrix_upper_triangular
correlation_matrix += matrix_upper_triangular.transpose()