csv文件在相关矩阵

时间:2018-05-01 02:09:53

标签: python csv pearson-correlation reorderlist

我想基于相关矩阵(在csv文件上计算)对csv文件的列重新排序。我使用了IRIS数据集并获得了以下相关矩阵

       sepal_length  sepal_width  petal_length  petal_width

sepal_length 1.000000 -0.109369 0.871754 0.817954 sepal_width -0.109369 1.000000 -0.420516 -0.356544 petal_length 0.871754 -0.420516 1.000000 0.962757 petal_width 0.817954 -0.356544 0.962757 1.000000

现在我想重新排列csv列,以便高相关列彼此相邻。稍后我会根据新的csv生成图表。 这是我尝试解决我的问题的代码,但此解决方案不适用于大型csv文件data = pd.read_csv('data.csv') if ((a['sepal_length']['sepal_width']) < (a['sepal_length']['petal_length']))and(a['sepal_length']['sepal_width']) < (a['sepal_length']['petal_width'])and (a['sepal_length']['petal_width']) < (a['sepal_length']['petal_length']): df1_reorder = df1[['sepal_length', 'petal_length', 'sepal_width', 'petal_width', 'species']] # rearrange column here df1_reorder.to_csv('sample_reorder3.csv', index=False)

0 个答案:

没有答案