def pivot(self, columns, rows, values=None, collect=None, zero=None):
if collect is not None and values is None:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if values is not None and collect is None:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>columns = self._as_label(columns)<EOL>rows = self._as_labels(rows)<EOL>if values is None:<EOL><INDENT>selected = self.select([columns] + rows)<EOL><DEDENT>else:<EOL><INDENT>selected = self.select([columns, values] + rows)<EOL><DEDENT>grouped = selected.groups([columns] + rows, collect)<EOL>rows_values = sorted(list(set(self.select(rows).rows)))<EOL>pivoted = type(self)(rows).with_rows(rows_values)<EOL>by_columns = grouped.index_by(columns)<EOL>for label in sorted(by_columns):<EOL><INDENT>tuples = [t[<NUM_LIT:1>:] for t in by_columns[label]] <EOL>column = _fill_with_zeros(rows_values, tuples, zero)<EOL>pivot = self._unused_label(str(label))<EOL>pivoted[pivot] = column<EOL><DEDENT>return pivoted<EOL>
Generate a table with a column for each unique value in ``columns``,
        with rows for each unique value in ``rows``. Each row counts/aggregates
        the values that match both row and column based on ``collect``.

        Args:
            ``columns`` -- a single column label or index, (``str`` or ``int``),
                used to create new columns, based on its unique values.
            ``rows`` -- row labels or indices, (``str`` or ``int`` or list),
                used to create new rows based on it's unique values.
            ``values`` -- column label in table for use in aggregation.
                Default None.
            ``collect`` -- aggregation function, used to group ``values``
                over row-column combinations. Default None.
            ``zero`` -- zero value for non-existent row-column combinations.

        Raises:
            TypeError -- if ``collect`` is passed in and ``values`` is not,
                vice versa.

        Returns:
            New pivot table, with row-column combinations, as specified, with
            aggregated ``values`` by ``collect`` across the intersection of
            ``columns`` and ``rows``. Simple counts provided if values and
            collect are None, as default.

        >>> titanic = Table().with_columns('age', make_array(21, 44, 56, 89, 95
        ...    , 40, 80, 45), 'survival', make_array(0,0,0,1, 1, 1, 0, 1),
        ...    'gender',  make_array('M', 'M', 'M', 'M', 'F', 'F', 'F', 'F'),
        ...    'prediction', make_array(0, 0, 1, 1, 0, 1, 0, 1))
        >>> titanic
        age  | survival | gender | prediction
        21   | 0        | M      | 0
        44   | 0        | M      | 0
        56   | 0        | M      | 1
        89   | 1        | M      | 1
        95   | 1        | F      | 0
        40   | 1        | F      | 1
        80   | 0        | F      | 0
        45   | 1        | F      | 1
        >>> titanic.pivot('survival', 'gender')
        gender | 0    | 1
        F      | 1    | 3
        M      | 3    | 1
        >>> titanic.pivot('prediction', 'gender')
        gender | 0    | 1
        F      | 2    | 2
        M      | 2    | 2
        >>> titanic.pivot('survival', 'gender', values='age', collect = np.mean)
        gender | 0       | 1
        F      | 80      | 60
        M      | 40.3333 | 89
        >>> titanic.pivot('survival', make_array('prediction', 'gender'))
        prediction | gender | 0    | 1
        0          | F      | 1    | 1
        0          | M      | 2    | 0
        1          | F      | 0    | 2
        1          | M      | 1    | 1
        >>> titanic.pivot('survival', 'gender', values = 'age')
        Traceback (most recent call last):
           ...
        TypeError: values requires collect to be specified
        >>> titanic.pivot('survival', 'gender', collect = np.mean)
        Traceback (most recent call last):
           ...
        TypeError: collect requires values to be specified