3

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.

3
  • 1
    Could you paste some sample representation of csvdata ? Commented Nov 19, 2012 at 11:06
  • 2
    numpy.sort Commented Nov 19, 2012 at 11:07
  • Yup.. I just added how it looks to the description. Commented Nov 19, 2012 at 11:11

1 Answer 1

4

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'])
Sign up to request clarification or add additional context in comments.

1 Comment

@gran_profaci You're welcome - I would highly recommend you go through a numpy tutorial though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.