python - 从字典创建多维数组 (D>5)..?

标签 python arrays dictionary multidimensional-array

我正在尝试使用不同长度的向量构建一个多维数组来映射问题的“过程空间”。我首先将值存储在字典的键中:

d = {'width' : [1,2,3,5,3,5,3],
 'height' : [1,2,3,5,5,3],
 'length' : [1,3,3,7,8,0,0,7,2,3,6,3,2,3],
 'composition' : [1,2,3,5,5,3],
 'year' : [7,5,3,2,1,6,4,9,11],
 'efficiency' : [1,1,2,3,5,8,13,21,34]}

是否可以使用这些键来构造大小为 的多维(6D)矩阵

(7,6,14,6,9,9)? (也就是说,每个字典键将表示为最终数组的单独维度)

编辑: 我想使用这个矩阵作为查看数据横截面的方法。例如,我希望能够说,“以下是作为‘长度’函数的所有效率值,给定:

width = 4
height = 2
composition = 3
year = 7

最佳答案

我认为您将列命名为维度。

既然你有索引和数据,就使用pandas DataFrames

from pandas import Series, DataFrame
d = {'width' : [1,2,3,5,3,5,3],
 'height' : [1,2,3,5,5,3],
 'length' : [1,3,3,7,8,0,0,7,2,3,6,3,2,3],
 'composition' : [1,2,3,5,5,3],
 'year' : [7,5,3,2,1,6,4,9,11],
 'efficiency' : [1,1,2,3,5,8,13,21,34]}

由于缺少数据,您需要一个中间步骤,直到可以将其转换为 DataFrame。

intermediate=dict()
for x in d:
    intermediate[x]=Series(d[x])

data=DataFrame(intermediate)

然后你可以使用普通的 pandas 语法查询数据。

data[data.length>5]

    composition  efficiency  height  length  width  year
3             5           3       5       7      5     2
4             5           5       5       8      3     1
7           NaN          21     NaN       7    NaN     9
10          NaN         NaN     NaN       6    NaN   NaN

关于python - 从字典创建多维数组 (D>5)..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34248145/

相关文章:

javascript - 使用参数数组调用 String.prototype.split

javascript - Parse.com JS 使用 query.find() 时循环遍历数组

javascript - 基于对象内部数组的新对象数组?

python - 访问变量字典名称的 Python 3x 字典值

python - 缩小 Matplotlib

python - 将 Markdown 渲染为 HTML,同时保留标题、换行符等?

Python:访问列表中的字典,更好的方法

ios - 在 Swift 中制作字典

python - 使用集合创建列会复制集合 n 次

python - 如何防止 lambda 函数局部变量在 python 中更改?