获取 python numpy ndarray 的列名 [英] Get the column names of a python numpy ndarray

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

问题描述

假设我有一个名为 data.txt 的数据文件,它看起来像:

TIME FX FY FZ0 10 5 61 2 4 72 5 2 6...

在 python 中运行:

将 numpy 导入为 npmyData = np.genfromtxt("data.txt", names=True)>>>打印 myData["TIME"][0, 1, 2]

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

<预><代码>>>>打印 myData.names[时间,F0,F1,F2]

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

解决方案

尝试:

myData.dtype.names

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

在 [10]: myData.dtype.names输出 [10]: ('TIME', 'FX', 'FY', 'FZ')

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
...

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.

解决方案

Try:

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 的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆