首页 > 解决方案 > 在 Excel 或 Jupyter 中对范围内的相关矩阵进行排序

问题描述

我有一个使用以下代码导出的大数据集:

(corr.style.background_gradient(cmap='coolwarm').to_excel("S:.......ABC.xlsx", engine="openpyxl"))

我如何拥有介于 1 到 -1 之间的大量数据,而我只想要范围为 0.3 到 0.933 和 -0.3 到 -0.933 的数据。

我该怎么做?

例如数据:

数据集

标签: pythonpandascorrelation

解决方案


您可以首先找到列以保持所有值都在这样的范围内

columns_to_keep1 = [col for col in corr.columns if not (any(corr[col] < 0.3) or any(corr[col] > 0.933))]
columns_to_keep2 = [col for col in corr.columns if not (any(corr[col] > -0.3) or any(corr[col] < -0.933))]

上面的代码片段将检查数据框中的所有列,并仅将所有值都在所需范围内的那些列添加到最终列表(通过列表理解)。

然后你可以columns_to_keep像这样只从你的数据框中选择列

corr = corr[columns_to_keep1 + columns_to_keep2]

推荐阅读