1from __future__ import absolute_import, print_function, division
2
3
4# melt()
5########
6
7import petl as etl
8table1 = [['id', 'gender', 'age'],
9          [1, 'F', 12],
10          [2, 'M', 17],
11          [3, 'M', 16]]
12table2 = etl.melt(table1, 'id')
13table2.lookall()
14# compound keys are supported
15table3 = [['id', 'time', 'height', 'weight'],
16          [1, 11, 66.4, 12.2],
17          [2, 16, 53.2, 17.3],
18          [3, 12, 34.5, 9.4]]
19table4 = etl.melt(table3, key=['id', 'time'])
20table4.lookall()
21# a subset of variable fields can be selected
22table5 = etl.melt(table3, key=['id', 'time'],
23                  variables=['height'])
24table5.lookall()
25
26
27# recast()
28##########
29
30import petl as etl
31table1 = [['id', 'variable', 'value'],
32          [3, 'age', 16],
33          [1, 'gender', 'F'],
34          [2, 'gender', 'M'],
35          [2, 'age', 17],
36          [1, 'age', 12],
37          [3, 'gender', 'M']]
38table2 = etl.recast(table1)
39table2
40# specifying variable and value fields
41table3 = [['id', 'vars', 'vals'],
42          [3, 'age', 16],
43          [1, 'gender', 'F'],
44          [2, 'gender', 'M'],
45          [2, 'age', 17],
46          [1, 'age', 12],
47          [3, 'gender', 'M']]
48table4 = etl.recast(table3, variablefield='vars', valuefield='vals')
49table4
50# if there are multiple values for each key/variable pair, and no
51# reducers function is provided, then all values will be listed
52table6 = [['id', 'time', 'variable', 'value'],
53          [1, 11, 'weight', 66.4],
54          [1, 14, 'weight', 55.2],
55          [2, 12, 'weight', 53.2],
56          [2, 16, 'weight', 43.3],
57          [3, 12, 'weight', 34.5],
58          [3, 17, 'weight', 49.4]]
59table7 = etl.recast(table6, key='id')
60table7
61# multiple values can be reduced via an aggregation function
62def mean(values):
63    return float(sum(values)) / len(values)
64
65table8 = etl.recast(table6, key='id', reducers={'weight': mean})
66table8
67# missing values are padded with whatever is provided via the
68# missing keyword argument (None by default)
69table9 = [['id', 'variable', 'value'],
70          [1, 'gender', 'F'],
71          [2, 'age', 17],
72          [1, 'age', 12],
73          [3, 'gender', 'M']]
74table10 = etl.recast(table9, key='id')
75table10
76
77
78# transpose()
79#############
80
81import petl as etl
82table1 = [['id', 'colour'],
83          [1, 'blue'],
84          [2, 'red'],
85          [3, 'purple'],
86          [5, 'yellow'],
87          [7, 'orange']]
88table2 = etl.transpose(table1)
89table2
90
91
92# pivot()
93#########
94
95import petl as etl
96table1 = [['region', 'gender', 'style', 'units'],
97          ['east', 'boy', 'tee', 12],
98          ['east', 'boy', 'golf', 14],
99          ['east', 'boy', 'fancy', 7],
100          ['east', 'girl', 'tee', 3],
101          ['east', 'girl', 'golf', 8],
102          ['east', 'girl', 'fancy', 18],
103          ['west', 'boy', 'tee', 12],
104          ['west', 'boy', 'golf', 15],
105          ['west', 'boy', 'fancy', 8],
106          ['west', 'girl', 'tee', 6],
107          ['west', 'girl', 'golf', 16],
108          ['west', 'girl', 'fancy', 1]]
109table2 = etl.pivot(table1, 'region', 'gender', 'units', sum)
110table2
111table3 = etl.pivot(table1, 'region', 'style', 'units', sum)
112table3
113table4 = etl.pivot(table1, 'gender', 'style', 'units', sum)
114table4
115
116
117# flatten()
118###########
119
120import petl as etl
121table1 = [['foo', 'bar', 'baz'],
122          ['A', 1, True],
123          ['C', 7, False],
124          ['B', 2, False],
125          ['C', 9, True]]
126list(etl.flatten(table1))
127
128
129# unflatten()
130#############
131
132import petl as etl
133a = ['A', 1, True, 'C', 7, False, 'B', 2, False, 'C', 9]
134table1 = etl.unflatten(a, 3)
135table1
136# a table and field name can also be provided as arguments
137table2 = [['lines'],
138          ['A'],
139          [1],
140          [True],
141          ['C'],
142          [7],
143          [False],
144          ['B'],
145          [2],
146          [False],
147          ['C'],
148          [9]]
149table3 = etl.unflatten(table2, 'lines', 3)
150table3
151