gran_profaci
gran_profaci

Reputation: 8461

Sorting from .csv file in Python

So I have a csv file that I entered using the following code:

csvdata = np.loadtxt(sys.argv[2],
                     delimiter=',',
                     dtype={
                            'names': ('year', 'month', 'day', 'ItemName'), 
                            'formats': ('i4', 'i4', 'i4', 'S10')
                           }
                    )

Now, I wanted to sort this data based on year, month and day. Can someone tell me how to do this????

Csv Data looks like this:

2012,3,6,ABCD
2012,3,6,XYZA

The thing is, it is currently getting sorted on the name. I wanted it on the date.

Upvotes: 3

Views: 1270

Answers (1)

Jon Clements
Jon Clements

Reputation: 142186

It's in the manual (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')])

So, you want:

np.sort(csvdata, order=['year', 'month', 'day'])

Upvotes: 4

Related Questions