示例#1
0
文件: examples.py 项目: datamade/petl
# recordreduce

table1 = [['foo', 'bar'],
          ['a', 3],
          ['a', 7],
          ['b', 2],
          ['b', 1],
          ['b', 9],
          ['c', 4]]

from petl import recordreduce, look    
look(table1)
def sumbar(key, records):
    return [key, sum([rec['bar'] for rec in records])]

table2 = recordreduce(table1, key='foo', reducer=sumbar, fields=['foo', 'barsum'])
look(table2)


# mergeduplicates

table1 = [['foo', 'bar', 'baz'],
          ['A', 1, 2.7],
          ['B', 2, None],
          ['D', 3, 9.4],
          ['B', None, 7.8],
          ['E', None, 42.],
          ['D', 3, 12.3],
          ['A', 2, None]]

from petl import mergeduplicates, look    
示例#2
0
# recordreduce

table1 = [['foo', 'bar'],
          ['a', 3],
          ['a', 7],
          ['b', 2],
          ['b', 1],
          ['b', 9],
          ['c', 4]]

from petl import recordreduce, look    
look(table1)
def sumbar(key, records):
    return [key, sum([rec['bar'] for rec in records])]

table2 = recordreduce(table1, key='foo', reducer=sumbar, fields=['foo', 'barsum'])
look(table2)


# mergeduplicates

table1 = [['foo', 'bar', 'baz'],
          ['A', 1, 2.7],
          ['B', 2, None],
          ['D', 3, 9.4],
          ['B', None, 7.8],
          ['E', None, 42.],
          ['D', 3, 12.3],
          ['A', 2, None]]

from petl import mergeduplicates, look