import pandas as pd def sort_matrix_by_diag (rotated_components_df): # 调整因子列顺序:按方差解释降序排列 variance_explained = (rotated_components_df ** 2).sum() column_order = variance_explained.sort_values(ascending=False).index.tolist() rotated_components_df = rotated_components_df[column_order] # 对载荷矩阵进行排序 sorted_loadings = rotated_components_df.copy() # 确定每个变量的主因子(绝对值最大的列) sorted_loadings['主因子'] = sorted_loadings.abs().idxmax(axis=1) # 将主因子转换为按列顺序的分类变量,确保排序正确 sorted_loadings['主因子'] = pd.Categorical( sorted_loadings['主因子'], categories=column_order, ordered=True ) # 提取每个变量在其主因子上的载荷绝对值 sorted_loadings['绝对值'] = sorted_loadings.apply( lambda row: abs(row[row['主因子']]), axis=1 ) # 按主因子顺序和绝对值降序排序 sorted_loadings = sorted_loadings.sort_values( by=['主因子', '绝对值'], ascending=[True, False] ) # 删除临时列并输出结果 sorted_loadings = sorted_loadings.drop(['主因子', '绝对值'], axis=1) return sorted_loadings