python - 按一列降序对 2by2 Numpy 数组进行排序
问题描述
我正在尝试对 2 x 2 Numpy 数组进行某种优雅的排序,以便字母继续与其受尊重的起始浮点值分组。但是浮点值应该从高到低排序。
this = {"z": 1.6, "aaaaaaaaaaaaa": 0, "w": 6, "v": 4}
orThis = [['z' '1.6']
['aaaaaaaaaaaaa' '0']
['w' '6']
['v' '4']]
shouldBecomeThis = [['w', 6. ],
['v', 4. ],
['z', 1.6],
['aaaaaaaaaaaaa', 0 ]]
结果应该看起来完全像这样的原因是因为我想将它输入到熊猫数据框中
import pandas as pd
def plotTable(data, header):
fig, ax = plt.subplots()
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
df = pd.DataFrame(data, columns=["gene", header])
#top line throws error if i feed it data= [('w', 6. ) ('v', 4. ) ('z', 1.6) ('aaaaaaaaaaaaa', 0. )]
ax.table(cellText=df.values, colLabels=df.columns, loc='center')
fig.tight_layout()
plt.show()
foo = [['w', 6. ],
['v', 4. ],
['z', 1.6],
['aaaaaaaaaaaaa', 0 ]]
plotTable(foo, "SomeTableHeader")
#Plots a table.
sortData = {"z": 1.6, "aaaaaaaaaaaaa": 0, "w": 6, "v": 4}
npArray = np.array(list(sortData.items()))
sortData = np.array(list(sortData.items()), dt)
sortData.view('<U16,<f8').sort(order=['f1'], axis=0)
sortData = np.flip(sortData, 0)
print(sortData)
#best i got so far: [('w', 6. ) ('v', 4. ) ('z', 1.6) ('aaaaaaaaaaaaa', 0. )]
我已经查看了这个,但它无法使它工作:Sorting arrays in NumPy by column
解决方案
import numpy as np
this = {"z": 1.6, "aaaaaaaaaaaaa": 0, "w": 6, "v": 4}
array=np.array([[key,val] for (key,val) in this.items()])
sortedArr = array[array[:,1].argsort()[::-1]]
print(sortedArr)
推荐阅读
- vbscript - 错误“80040211”使用 CDO.Message 在 Windows Server 2012 R2 上发送电子邮件
- sql - 从单个表中返回 0 值和 Oracle 中的计数
- javascript - JQuery - 用 fa fa-star 计算 i 的数量
- r - 更改 R data.table 聚合中的默认列名
- javascript - 如何在 HTML 标签上打印/显示动态值?
- aws-cognito - Pivotal Cloud Foundry (PCF) 上的 AWS Cognito 替代品
- python - 导入不规则大小的文本文件
- python - 得到 IndexError 时使数组重复
- python - tensorflow gpu 安装失败
- java - 是否可以换出用于运行 Java 的 JavaExecAction Gradle?