Skip to content

Transposerows

flowtask.components.TransposeRows

TransposeRows

TransposeRows(loop=None, job=None, stat=None, **kwargs)

Bases: FlowComponent

TransposeRows

Overview

 The TransposeRows class is a component for transposing specified rows in a DataFrame by converting row values
 into new columns based on pivot settings. This component supports options for preserving the original data,
 handling empty results, and custom column configurations for the transposition.

.. table:: Properties :widths: auto

 +------------------+----------+-----------+---------------------------------------------------------------+
 | Name             | Required | Summary                                                                   |
 +------------------+----------+-----------+---------------------------------------------------------------+
 | pivot            |   Yes    | List of columns to use as the pivot index for transposition.              |
 +------------------+----------+-----------+---------------------------------------------------------------+
 | columns          |   Yes    | Dictionary mapping row values to their target column names.               |
 +------------------+----------+-----------+---------------------------------------------------------------+
 | preserve_original|   No     | Boolean indicating if the original rows should be preserved.              |
 +------------------+----------+-----------+---------------------------------------------------------------+
 | allow_empty      |   No     | Boolean indicating if empty columns should be allowed in the output.      |
 +------------------+----------+-----------+---------------------------------------------------------------+

Returns

 This component returns a DataFrame with specified rows transposed into columns according to the provided pivot
 and column configurations. If `preserve_original` is set to False, the original rows used in transposition
 are removed. Any errors in column mapping or pivoting are raised with descriptive error messages.


 Example:

 ```yaml
 TransposeRows:
   column: column_name
   value: data
   pivot:
   - formid
   - form_id
   - orgid
   preserve_original: true
   allow_empty: true
   columns:
     '000_001': ad_hoc
     '000_003': creation_timestamp
     '000_004': user_device
     000_008: geoloc
     000_009: visit_length
     '000_012': store_name
     '000_013': store_id
     '000_023': account_name
     '000_024': store_designation
     '000_026': region_name
     000_029: store_timezone
     '000_037': visitor_username
     000_038: visitor_name
     000_039: visitor_email
     '000_045': visitor_role
     '000_055': updated_timestamp
     '000_065': activity_item_id
     '000_063': time_in
     '000_064': time_out
     '000_066': position_id
     '000_067': position_manager
     '000_070': visit_status
     '194834': retailer
     VisitDateLocal: visit_timestamp
 ```

row_to_column

row_to_column(df, row_to_pivot, new_name)

Add a pivoted column to the dataframe based on the given column name.

Parameters: - df: The input dataframe. - row_to_pivot: The column name to be pivoted. - new_name: The name of the column to be transposed.

Returns: - Dataframe with the new pivoted column.