Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 516
gran_profaci Avatar asked Aug 14 '26 19:08

gran_profaci


1 Answers

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'])
like image 197
Jon Clements Avatar answered Aug 17 '26 09:08

Jon Clements



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!