python - 使用字典/表的第一列作为索引/键

标签 python dictionary astropy

我正在尝试将 astropy.table 用作二维数组,但我也想将第一列用作索引。

例如,我已经有如下表格:

 a   b  c  d
'1t'  2  3  4
'5t'  6  7  8
'9t'  0  10 11

这样 打印表['b'] = 2,6,0

我想要的是选择一个元素,这样 打印表['b']['5t'] = 6

除了使用 table['b'][1] = 6 之外,还有其他方法吗?

最佳答案

我用这个例子展示了一个替代解决方案:

from astropy.table import Table, Column
import numpy as np
t = Table([['4t', '5t', '6t'], [0.1, 0.2, 0.3],[10, 20, 30]], names=('a', 'b','c'))
print (t)

输出:

 a   b   c 
--- --- ---
 4t 0.1  10
 5t 0.2  20
 6t 0.3  30

设置索引并使用loc:

t.add_index('a')
T=t.loc
print (T['4t']['b'])

输出:

0.1

关于python - 使用字典/表的第一列作为索引/键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57422587/

相关文章:

python - 如何使用 web3.py 在钱包之间传输 ERC20 代币

file - 如何在 Clojure 中从文件中读取多个变量?

python - 适合 WCS 来治愈

xcode - swift 2.0 : Type of expression is ambiguous without more context (Using Parse)

python - 如何用pyfits合并两个表?

python - 如何在astropy,python中通过 "pillars of creation"创建 "make_lupton_rgb of astropy"的良好图像?

python - 在Python中对包含数字的字符串列表进行排序

python - 有没有一种优雅的方法来使用 pytest 测试 ray 远程功能?

python - 将 Pandas 列转换为日期时间

python - 我有一个包含字典的元组列表。如何编辑我的代码以在这些字典内的单独列表中查找值?