1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
from __future__ import division, print_function, absolute_import
# cut()
#######
import petl as etl
table1 = [['foo', 'bar', 'baz'],
['A', 1, 2.7],
['B', 2, 3.4],
['B', 3, 7.8],
['D', 42, 9.0],
['E', 12]]
table2 = etl.cut(table1, 'foo', 'baz')
table2
# fields can also be specified by index, starting from zero
table3 = etl.cut(table1, 0, 2)
table3
# field names and indices can be mixed
table4 = etl.cut(table1, 'bar', 0)
table4
# select a range of fields
table5 = etl.cut(table1, *range(0, 2))
table5
# cutout()
##########
import petl as etl
table1 = [['foo', 'bar', 'baz'],
['A', 1, 2.7],
['B', 2, 3.4],
['B', 3, 7.8],
['D', 42, 9.0],
['E', 12]]
table2 = etl.cutout(table1, 'bar')
table2
# cat()
#######
import petl as etl
table1 = [['foo', 'bar'],
[1, 'A'],
[2, 'B']]
table2 = [['bar', 'baz'],
['C', True],
['D', False]]
table3 = etl.cat(table1, table2)
table3
# can also be used to square up a single table with uneven rows
table4 = [['foo', 'bar', 'baz'],
['A', 1, 2],
['B', '2', '3.4'],
[u'B', u'3', u'7.8', True],
['D', 'xyz', 9.0],
['E', None]]
table5 = etl.cat(table4)
table5
# use the header keyword argument to specify a fixed set of fields
table6 = [['bar', 'foo'],
['A', 1],
['B', 2]]
table7 = etl.cat(table6, header=['A', 'foo', 'B', 'bar', 'C'])
table7
# using the header keyword argument with two input tables
table8 = [['bar', 'foo'],
['A', 1],
['B', 2]]
table9 = [['bar', 'baz'],
['C', True],
['D', False]]
table10 = etl.cat(table8, table9, header=['A', 'foo', 'B', 'bar', 'C'])
table10
# addfield()
############
import petl as etl
table1 = [['foo', 'bar'],
['M', 12],
['F', 34],
['-', 56]]
# using a fixed value
table2 = etl.addfield(table1, 'baz', 42)
table2
# calculating the value
table2 = etl.addfield(table1, 'baz', lambda rec: rec['bar'] * 2)
table2
# rowslice()
############
import petl as etl
table1 = [['foo', 'bar'],
['a', 1],
['b', 2],
['c', 5],
['d', 7],
['f', 42]]
table2 = etl.rowslice(table1, 2)
table2
table3 = etl.rowslice(table1, 1, 4)
table3
table4 = etl.rowslice(table1, 0, 5, 2)
table4
# head()
########
import petl as etl
table1 = [['foo', 'bar'],
['a', 1],
['b', 2],
['c', 5],
['d', 7],
['f', 42],
['f', 3],
['h', 90]]
table2 = etl.head(table1, 4)
table2
# tail()
########
import petl as etl
table1 = [['foo', 'bar'],
['a', 1],
['b', 2],
['c', 5],
['d', 7],
['f', 42],
['f', 3],
['h', 90],
['k', 12],
['l', 77],
['q', 2]]
table2 = etl.tail(table1, 4)
table2
# skipcomments()
################
import petl as etl
table1 = [['##aaa', 'bbb', 'ccc'],
['##mmm',],
['#foo', 'bar'],
['##nnn', 1],
['a', 1],
['b', 2]]
table2 = etl.skipcomments(table1, '##')
table2
# annex()
#########
import petl as etl
table1 = [['foo', 'bar'],
['A', 9],
['C', 2],
['F', 1]]
table2 = [['foo', 'baz'],
['B', 3],
['D', 10]]
table3 = etl.annex(table1, table2)
table3
# addrownumbers()
#################
import petl as etl
table1 = [['foo', 'bar'],
['A', 9],
['C', 2],
['F', 1]]
table2 = etl.addrownumbers(table1)
table2
# addcolumn()
#############
import petl as etl
table1 = [['foo', 'bar'],
['A', 1],
['B', 2]]
col = [True, False]
table2 = etl.addcolumn(table1, 'baz', col)
table2
# addfieldusingcontext()
########################
import petl as etl
table1 = [['foo', 'bar'],
['A', 1],
['B', 4],
['C', 5],
['D', 9]]
def upstream(prv, cur, nxt):
if prv is None:
return None
else:
return cur.bar - prv.bar
def downstream(prv, cur, nxt):
if nxt is None:
return None
else:
return nxt.bar - cur.bar
table2 = etl.addfieldusingcontext(table1, 'baz', upstream)
table3 = etl.addfieldusingcontext(table2, 'quux', downstream)
table3
|