本文介绍了获取python numpy ndarray的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个名为data.txt的数据文件,看起来像:

Let's say I have a data file called data.txt that looks like:

TIME FX FY FZ
0    10 5  6
1    2  4  7
2    5  2  6
...

在python中运行:

In python run:

import numpy as np

myData = np.genfromtxt("data.txt", names=True)

>>> print myData["TIME"]
[0, 1, 2]

数据文件顶部的名称会有所不同,所以我想做的就是找出数据文件中数组的名称.所以我想要这样的东西:

The names at the top of my data file will vary, so what I would like to do is find out what the names of my arrays in the data file are. So I would like something like:

>>> print myData.names
[TIME, F0, F1, F2]

我本来只是想读入数据文件并获得第一行并将其解析为一个单独的操作,但这似乎并不十分有效或优雅.

I thought about just to read in the data file and get the first line and parse it as a separate operation, but that doesn't seem very efficient or elegant.

推荐答案

尝试:

myData.dtype.names

这将返回字段名称的元组.

This will return a tuple of the field names.

In [10]: myData.dtype.names
Out[10]: ('TIME', 'FX', 'FY', 'FZ')

这篇关于获取python numpy ndarray的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:58