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
|
=========
Transform
=========
Pivot by a single column
========================
The :meth:`.Table.pivot` method is a general process for grouping data by row and, optionally, by column, and then calculating some aggregation for each group. Consider the following table:
+---------+---------+--------+-------+
| name | race | gender | age |
+=========+=========+========+=======+
| Joe | white | female | 20 |
+---------+---------+--------+-------+
| Jane | asian | male | 20 |
+---------+---------+--------+-------+
| Jill | black | female | 20 |
+---------+---------+--------+-------+
| Jim | latino | male | 25 |
+---------+---------+--------+-------+
| Julia | black | female | 25 |
+---------+---------+--------+-------+
| Joan | asian | female | 25 |
+---------+---------+--------+-------+
In the very simplest case, this table can be pivoted to count the number occurences of values in a column:
.. code-block:: python
transformed = table.pivot('race')
Result:
+---------+--------+
| race | pivot |
+=========+========+
| white | 1 |
+---------+--------+
| asian | 2 |
+---------+--------+
| black | 2 |
+---------+--------+
| latino | 1 |
+---------+--------+
Pivot by multiple columns
=========================
You can pivot by multiple columns either as additional row-groups, or as intersecting columns. For example, given the table in the previous example:
.. code-block:: python
transformed = table.pivot(['race', 'gender'])
Result:
+---------+--------+-------+
| race | gender | pivot |
+=========+========+=======+
| white | female | 1 |
+---------+--------+-------+
| asian | male | 1 |
+---------+--------+-------+
| black | female | 2 |
+---------+--------+-------+
| latino | male | 1 |
+---------+--------+-------+
| asian | female | 1 |
+---------+--------+-------+
For the column, version you would do:
.. code-block:: python
transformed = table.pivot('race', 'gender')
Result:
+---------+--------+--------+
| race | male | female |
+=========+========+========+
| white | 0 | 1 |
+---------+--------+--------+
| asian | 1 | 1 |
+---------+--------+--------+
| black | 0 | 2 |
+---------+--------+--------+
| latino | 1 | 0 |
+---------+--------+--------+
Pivot to sum
============
The default pivot aggregation is :class:`.Count` but you can also supply other operations. For example, to aggregate each group by :class:`.Sum` of their ages:
.. code-block:: python
transformed = table.pivot('race', 'gender', aggregation=agate.Sum('age'))
+---------+--------+--------+
| race | male | female |
+=========+========+========+
| white | 0 | 20 |
+---------+--------+--------+
| asian | 20 | 25 |
+---------+--------+--------+
| black | 0 | 45 |
+---------+--------+--------+
| latino | 25 | 0 |
+---------+--------+--------+
Pivot to percent of total
=========================
Pivot allows you to apply a :class:`.Computation` to each row of aggregated results prior to returning the table. Use the stringified name of the aggregation as the column argument to your computation:
.. code-block:: python
transformed = table.pivot('race', 'gender', aggregation=agate.Sum('age'), computation=agate.Percent('sum'))
+---------+--------+--------+
| race | male | female |
+=========+========+========+
| white | 0 | 14.8 |
+---------+--------+--------+
| asian | 14.8 | 18.4 |
+---------+--------+--------+
| black | 0 | 33.3 |
+---------+--------+--------+
| latino | 18.4 | 0 |
+---------+--------+--------+
*Note: actual computed percentages will be much more precise.*
It's helpful when constructing these cases to think of all the cells in the pivot table as a single sequence.
Denormalize key/value columns into separate columns
===================================================
It's common for very large datasets to be distributed in a "normalized" format, such as:
+---------+-----------+---------+
| name | property | value |
+=========+===========+=========+
| Jane | gender | female |
+---------+-----------+---------+
| Jane | race | black |
+---------+-----------+---------+
| Jane | age | 24 |
+---------+-----------+---------+
| ... | ... | ... |
+---------+-----------+---------+
The :meth:`.Table.denormalize` method can be used to transform the table so that each unique property has its own column.
.. code-block:: python
transformed = table.denormalize('name', 'property', 'value')
Result:
+---------+----------+--------+-------+
| name | gender | race | age |
+=========+==========+========+=======+
| Jane | female | black | 24 |
+---------+----------+--------+-------+
| Jack | male | white | 35 |
+---------+----------+--------+-------+
| Joe | male | black | 28 |
+---------+----------+--------+-------+
Normalize separate columns into key/value columns
=================================================
Sometimes you have a dataset where each property has its own column, but your analysis would be easier if all properties were stored together. Consider this table:
+---------+----------+--------+-------+
| name | gender | race | age |
+=========+==========+========+=======+
| Jane | female | black | 24 |
+---------+----------+--------+-------+
| Jack | male | white | 35 |
+---------+----------+--------+-------+
| Joe | male | black | 28 |
+---------+----------+--------+-------+
The :meth:`.Table.normalize` method can be used to transform the table so that all the properties and their values share two columns.
.. code-block:: python
transformed = table.normalize('name', ['gender', 'race', 'age'])
Result:
+---------+-----------+---------+
| name | property | value |
+=========+===========+=========+
| Jane | gender | female |
+---------+-----------+---------+
| Jane | race | black |
+---------+-----------+---------+
| Jane | age | 24 |
+---------+-----------+---------+
| ... | ... | ... |
+---------+-----------+---------+
|