toucan_data_sdk/utils/postprocess/pivot.py
def pivot(df, index: List[str], column: str, value: str, agg_function: str = 'mean'):
    """
    Pivot the data. Reverse operation of melting

    ---

    ### Parameters

    *mandatory :*
    - `index` (*list*): names of index columns.
    - `column` (*str*): column name to pivot on
    - `value` (*str*): column name containing the value to fill the pivoted df

    *optional :*
    - `agg_function` (*str*): aggregation function to use among 'mean' (default), 'count', 'mean', 'max', 'min'

    ---

    ### Example

    **Input**

    | variable |   wave  |  year    | value |
    |:--------:|:-------:|:--------:|:-----:|
    |   toto   |  wave 1 |  2014    |  300  |
    |   toto   |  wave 1 |  2015    |  250  |
    |   toto   |  wave 1 |  2016    |  450  |

    ```cson
    pivot:
      index: ['variable','wave']
      column: 'year'
      value: 'value'
    ```

    **Output**

    | variable |   wave  |  2014  | 2015 | 2015 |
    |:--------:|:-------:|:------:|:----:|:----:|
    |   toto   |  wave 1 |  300   | 250  | 450  |
    """
    if df.dtypes[value].type == np.object_:
        df = pd.pivot_table(df, index=index,
                            columns=column,
                            values=value,
                            aggfunc=lambda x: ' '.join(x))
    else:
        df = pd.pivot_table(df, index=index,
                            columns=column,
                            values=value,
                            aggfunc=agg_function)
    df = df.reset_index()
    return df
def pivot(df, index: List[str], column: str, value: str, agg_function: str = 'mean'):
    """
    Pivot the data. Reverse operation of melting

    ---

    ### Parameters

    *mandatory :*
    - `index` (*list*): names of index columns.
    - `column` (*str*): column name to pivot on
    - `value` (*str*): column name containing the value to fill the pivoted df

    *optional :*
    - `agg_function` (*str*): aggregation function to use among 'mean' (default), 'count', 'mean', 'max', 'min'

    ---

    ### Example

    **Input**

    | variable |   wave  |  year    | value |
    |:--------:|:-------:|:--------:|:-----:|
    |   toto   |  wave 1 |  2014    |  300  |
    |   toto   |  wave 1 |  2015    |  250  |
    |   toto   |  wave 1 |  2016    |  450  |

    ```cson
    pivot:
      index: ['variable','wave']
      column: 'year'
      value: 'value'
    ```

    **Output**

    | variable |   wave  |  2014  | 2015 | 2015 |
    |:--------:|:-------:|:------:|:----:|:----:|
    |   toto   |  wave 1 |  300   | 250  | 450  |
    """
    if df.dtypes[value].type == np.object_:
        df = pd.pivot_table(df, index=index,
                            columns=column,
                            values=value,
                            aggfunc=lambda x: ' '.join(x))
    else:
        df = pd.pivot_table(df, index=index,
                            columns=column,
                            values=value,
                            aggfunc=agg_function)
    df = df.reset_index()
    return df
Pivot the data. Reverse operation of melting

    ---

    ### Parameters

    *mandatory :*
    - `index` (*list*): names of index columns.
    - `column` (*str*): column name to pivot on
    - `value` (*str*): column name containing the value to fill the pivoted df

    *optional :*
    - `agg_function` (*str*): aggregation function to use among 'mean' (default), 'count', 'mean', 'max', 'min'

    ---

    ### Example

    **Input**

    | variable |   wave  |  year    | value |
    |:--------:|:-------:|:--------:|:-----:|
    |   toto   |  wave 1 |  2014    |  300  |
    |   toto   |  wave 1 |  2015    |  250  |
    |   toto   |  wave 1 |  2016    |  450  |

    ```cson
    pivot:
      index: ['variable','wave']
      column: 'year'
      value: 'value'
    ```

    **Output**

    | variable |   wave  |  2014  | 2015 | 2015 |
    |:--------:|:-------:|:------:|:----:|:----:|
    |   toto   |  wave 1 |  300   | 250  | 450  |