我有一个NumPy
ndarray
要添加行/列标题。
数据实际上是7x12x12,但我可以这样表示:
A=[[[0, 1, 2, 3, 4, 5],
[1, 0, 3, 4, 5, 6],
[2, 3, 0, 5, 6, 7],
[3, 4, 5, 0, 7, 8],
[4, 5, 6, 7, 0, 9],
[5, 6, 7, 8, 9, 0]]
[[0, 1, 2, 3, 4, 5],
[1, 0, 3, 4, 5, 6],
[2, 3, 0, 5, 6, 7],
[3, 4, 5, 0, 7, 8],
[4, 5, 6, 7, 0, 9],
[5, 6, 7, 8, 9, 0]]]
其中A是我的2x6x6阵列。
如何在第一行和第一列之间插入头,使每个数组在我的CSV
输出文件中都是这样的?
A, a, b, c, d, e, f
a, 0, 1, 2, 3, 4, 5,
b, 1, 0, 3, 4, 5, 6,
c, 2, 3, 0, 5, 6, 7,
d, 3, 4, 5, 0, 7, 8,
e, 4, 5, 6, 7, 0, 9,
f, 5, 6, 7, 8, 9, 0
现在,我所做的是制作数组7x13x13并插入数据,这样我就有一行和一列零,但我更喜欢字符串。
我想我可以写一个Excel宏,用字符串替换0。但是,问题是NumPy
无法将string
转换为float
,如果我尝试将这些零重新分配为所需的字符串。
使用^{} 可以将列和索引写入文件:
将提供以下
df.csv
文件:认为这是一般的伎俩
输入
代码
输出(存储在mats中的值):
Numpy将处理n维数组,但5月份的设备仅限于二维数组。甚至不确定输出文件的外观。
许多希望命名列的人忽略了numpy的recarray()功能。好东西知道,但那只是“名字”一个维度。
对于二维空间,Pandas非常酷。
如果输出是您在这里试图解决的唯一问题,我可能会坚持使用几行手工编码的魔术,因为它比为一个功能安装另一个包要轻。
相关问题 更多 >
编程相关推荐