+-

所以我有一个使用以下代码输入的csv文件:
csvdata = np.loadtxt(sys.argv[2],
delimiter=',',
dtype={
'names': ('year', 'month', 'day', 'ItemName'),
'formats': ('i4', 'i4', 'i4', 'S10')
}
)
现在,我想根据年,月和日对这些数据进行排序.有人可以告诉我该怎么做吗????
Csv数据如下所示:
2012,3,6,ABCD
2012,3,6,XYZA
事实是,它目前正在按名称排序.我想要约会
最佳答案
在手册中(http://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html)
Use the order keyword to specify a field to use when sorting a
structured array:
>>> dtype = [('name', 'S10'), ('height', float), ('age', int)]
>>> values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38),
... ('Galahad', 1.7, 38)]
>>> a = np.array(values, dtype=dtype) # create a structured array
>>> np.sort(a, order='height')
array([('Galahad', 1.7, 38), ('Arthur', 1.8, 41),
('Lancelot', 1.8999999999999999, 38)],
dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])
所以你要:
np.sort(csvdata, order=['year', 'month', 'day'])
点击查看更多相关文章
转载注明原文:从Python中的.csv文件排序 - 乐贴网