cudf.DataFrame.pivot#

DataFrame.pivot(*, columns, index=_NoDefault.no_default, values=_NoDefault.no_default)[source]#

Return reshaped DataFrame organized by the given index and column values.

Reshape data (produce a “pivot” table) based on column values. Uses unique values from specified index / columns to form axes of the resulting DataFrame.

Parameters:
columnscolumn name, optional

Column used to construct the columns of the result.

indexcolumn name, optional

Column used to construct the index of the result.

valuescolumn name or list of column names, optional

Column(s) whose values are rearranged to produce the result. If not specified, all remaining columns of the DataFrame are used.

Returns:
DataFrame

Examples

>>> a = cudf.DataFrame()
>>> a['a'] = [1, 1, 2, 2]
>>> a['b'] = ['a', 'b', 'a', 'b']
>>> a['c'] = [1, 2, 3, 4]
>>> a.pivot(index='a', columns='b')
   c
b  a  b
a
1  1  2
2  3  4

Pivot with missing values in result:

>>> a = cudf.DataFrame()
>>> a['a'] = [1, 1, 2]
>>> a['b'] = [1, 2, 3]
>>> a['c'] = ['one', 'two', 'three']
>>> a.pivot(index='a', columns='b')
          c
    b     1     2      3
    a
    1   one   two   <NA>
    2  <NA>  <NA>  three