I have a dataset in Pytables, which looks something like
class myData(IsDescription):
date = StringCol(16)
item = Int32Col()
I have multiple items per same date, for example:
'2010-01-01', 5
'2010-01-01', 6
'2010-01-02', 7
'2010-01-02', 8
Is there a way to iterate over unique dates and then over items in the date? I mean something like
for date in DATE
print date
for ITEM
print item
I am not familiar with the inner-workings of Pytables (so this may not be in-line with what you are looking for), but the
groupbyfunction in theitertoolsmodule is very useful in these types of situations (note the sorting step below – this is important in this case in order to getgroupbyto group all items with the same date. See here for more info.):The basic pattern here is to get a list/container that holds the objects you want to group. You then sort that list based by the attribute by which we will later be grouping (in this case,
date). You then pass that sorted list to thegroupbyfunction, which will emit two values in each iteration – akeythat represents the value by which you grouped (so here it will be thedateof each group) and agroupiterator that contains all of your objects that share that samedatekey. You can then iterate through that group, pulling out theitemattribute of each object.