所以我有一个csv文件,我用以下代码输入:
csvdata = np.loadtxt(sys.argv[2],
delimiter=',',
dtype={
'names': ('year', 'month', 'day', 'ItemName'),
'formats': ('i4', 'i4', 'i4', 'S10')
}
)
现在,我想根据年份、月份和日期对这些数据进行排序有人能告诉我怎么做吗???是吗?
CSV数据如下:
2012年3月6日
2012年3月6日
问题是,它目前正在按名称排序我想在约会的时候买。
最佳答案
它在手册中(http://doc s.scipy.org/doc/numpy/reference/generated/numpy.sort.html)
使用order关键字指定排序时要使用的字段
结构化阵列:
>>> 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'])