我可以重命名 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.