我可以重命名 numpy 记录数组中的字段吗 [英] Can I rename fields in a numpy record array

查看:89
本文介绍了我可以重命名 numpy 记录数组中的字段吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,所以这听起来可能很基础.我已经使用csv2rec导入了一个csv文件.第一行有标题.我想将标题更改为"x","y","z".这样做的最佳方法是什么?

I am new to python so this may sound very basic. I have imported a csv file using csv2rec. The first row has headers. I want to change the headers to 'x', 'y', 'z'. What's the best way of doing this?

>>> import matplotlib
>>> import matplotlib.mlab as mlab
>>> r= mlab.csv2rec('HeightWeight.csv', delimiter= ',')
>>> names= r.dtype.names
>>> for i in names:
     print i


index
heightinches
weightpounds

推荐答案

mlab.csv2rec 有一个 names 参数,你可以用它来设置列名:

mlab.csv2rec has a names parameter which you can use to set the column names:

r= mlab.csv2rec('HeightWeight.csv', delimiter= ',', 
                 names=['apple', 'pear'], 
                 skiprows=1)

names 不是 None 时,csv2rec 假定没有标题行.因此,使用 skiprows = 1 忽略标题行.

When names is not None, csv2rec assumes there is no header row. So use skiprows=1 to ignore the header row.

这篇关于我可以重命名 numpy 记录数组中的字段吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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