# rangeaggregate table1 = [['foo', 'bar'], ['a', 3], ['a', 7], ['b', 2], ['b', 1], ['b', 9], ['c', 4], ['d', 3]] from petl import rangeaggregate, look, strjoin look(table1) # aggregate whole rows table2 = rangeaggregate(table1, 'bar', 2, len) look(table2) # aggregate single field table3 = rangeaggregate(table1, 'bar', 2, list, 'foo') look(table3) # aggregate single field - alternative signature using keyword args table4 = rangeaggregate(table1, key='bar', width=2, aggregation=list, value='foo') look(table4) # aggregate multiple fields from collections import OrderedDict aggregation = OrderedDict() aggregation['foocount'] = len aggregation['foojoin'] = 'foo', strjoin('') aggregation['foolist'] = 'foo' # default is list table5 = rangeaggregate(table1, 'bar', 2, aggregation) look(table5)
from petl import rangecounts, look look(table1) table2 = rangecounts(table1, 'bar', width=2) look(table2) # rangeaggregate table1 = [['foo', 'bar'], ['a', 3], ['a', 7], ['b', 2], ['b', 1], ['b', 9], ['c', 4], ['d', 3]] from petl import rangeaggregate, look look(table1) table2 = rangeaggregate(table1, 'bar', width=2) table2['foocount'] = 'foo', len table2['foolist'] = 'foo' # default is list look(table2) # rowmap table1 = [['id', 'sex', 'age', 'height', 'weight'], [1, 'male', 16, 1.45, 62.0], [2, 'female', 19, 1.34, 55.4], [3, 'female', 17, 1.78, 74.4], [4, 'male', 21, 1.33, 45.2], [5, '-', 25, 1.65, 51.9]] from petl import rowmap, look look(table1)
# rangeaggregate table1 = [['foo', 'bar'], ['a', 3], ['a', 7], ['b', 2], ['b', 1], ['b', 9], ['c', 4], ['d', 3]] from petl import rangeaggregate, look look(table1) table2 = rangeaggregate(table1, 'bar', width=2) table2['foocount'] = 'foo', len table2['foolist'] = 'foo' # default is list look(table2) # rowmap table1 = [['id', 'sex', 'age', 'height', 'weight'], [1, 'male', 16, 1.45, 62.0], [2, 'female', 19, 1.34, 55.4], [3, 'female', 17, 1.78, 74.4], [4, 'male', 21, 1.33, 45.2], [5, '-', 25, 1.65, 51.9]] from petl import rowmap, look