On 3/22/12 1:59 PM, Francesc Alted wrote:
On 3/22/12 12:48 PM, sreeaurovindh viswanathan wrote:
But.. Can i get sort one column by descending and the other ascending.
say
if i have two columns and first i would like to sort the one in
ascending and then sort the second column based on the search from the
first.
I mean I i have
1 5
2 6
1 8
2 9
Could i get an output as
1 5
1 8
2 6
2 9
No, this is not supported by PyTables.
But hey, you can always make use of the sorted iterator, and the
additonal sorting by yourselves. In your example, let's suppose that
column 0 is named 'f0' and column 1 is named 'f1'. Then, the next loop:
prevval = None
gf1 = []
for r in t.itersorted('f0'):
if r['f0'] != prevval:
if gf1:
gf1.sort()
print prevval, gf1[::-1] # reverse sorted
prevval = r['f0']
gf1 = []
gf1.append(r['f1'])
if gf1:
gf1.sort()
print prevval, gf1[::-1] # reverse sorted
Hmm, I just realized that there it is another, equivalent code that
solves the same problem:
def field_selector(row):
return row['f0']
for field, rows_grouped_by_field in
itertools.groupby(t.itersorted('f0'), field_selector):
group = [ r['f1'] for r in rows_grouped_by_field ]
group.sort()
print '%s -> %s' % (field, group[::-1])
The performance of both is similar, so use whatever you find more useful.
For the record, I'm attaching a couple of self-contained examples that
exercises the different approaches.
-- Francesc Alted
import numpy as np
import tables
N = 1e5
f = tables.openFile('sortby.h5', 'w')
r = np.random.randint(0, 1e3, N*2).reshape(N,2)
sa = np.fromiter(((str(i), j) for i,j in r), dtype='S6,i4', count=int(N))
t = f.createTable('/', 'sa', sa)
print t[:10]
t.cols.f0.createCSIndex()
#print t.readSorted('f0', start=0, stop=10, step=-1)
prevval = None
gf1 = []
for r in t.itersorted('f0'):
if r['f0'] != prevval:
if gf1:
gf1.sort()
print prevval, gf1[::-1] # reverse sorted
prevval = r['f0']
gf1 = []
gf1.append(r['f1'])
if gf1:
gf1.sort()
print prevval, gf1[::-1] # reverse sorted
f.close()
import numpy as np
import itertools
import tables
N = 1e5
f = tables.openFile('sortby.h5', 'w')
r = np.random.randint(0, 1e3, N*2).reshape(N,2)
sa = np.fromiter(((str(i), j) for i,j in r), dtype='S6,i4', count=int(N))
t = f.createTable('/', 'sa', sa)
t.cols.f0.createCSIndex()
def field_selector(row):
return row['f0']
for field, rows_grouped_by_field in itertools.groupby(t.itersorted('f0'), field_selector):
group = [ r['f1'] for r in rows_grouped_by_field ]
group.sort()
print '%s -> %s' % (field, group[::-1])
f.close()
------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Pytables-users mailing list
Pytables-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pytables-users