piper.verbs.str_join

piper.verbs.str_join(df: pandas.core.frame.DataFrame, columns: Optional[List] = None, column: Optional[str] = None, sep: str = '', loc: str = 'after', drop: bool = True)pandas.core.frame.DataFrame[source]

join or combine columns with a separator

Join or combine a number of columns together into one column. Column(s) that are not of ‘string’ data type are automatically converted to strings then combined.

Note

If the column keyword value contains one of the combined columns, it will automatically replace the original column if drop=True.

If drop=False, the function will rename and append an underscore to the returned combined column name. See example for details:

Parameters
  • df – a pandas dataframe

  • columns – list of column names to join/combine

  • column – (optional) column name to store the results of combined columns

  • sep – (optional) separator value. Default is ‘’.

  • loc

    location to place the split column output within the dataframe Default is ‘after’ meaning place the output after the column.

    Valid locations are: ‘before’ or ‘after’ the column. You can also specify ‘first’ or ‘last’ corresponding to the first and last columns of the dataframe.

    For more information about relocating columns - see the relocate() function.

  • drop – drop original columns used in str_join function

Returns

Return type

A copy of the pandas dataframe

Examples

%%piper
sample_sales()
>> str_join(columns=['actual_sales', 'product', 'actual_profit'],
            sep='|',
            column='actual_sales',
            drop=True)
>> head(tablefmt='plain')

     location month               target_sales target_profit actual_sales
  4  London   2021-01-01 00:00:00        31749          1905 29209.08|Beachwear|1752.54
125  London   2021-01-01 00:00:00        37833          6053 34049.7|Beachwear|5447.95
 21  London   2021-01-01 00:00:00        29485          4128 31548.95|Jeans|4416.85
148  London   2021-01-01 00:00:00        37524          3752 40901.16|Jeans|4090.12

Same example, this time with drop=False, note the appended underscore in the combined column name.

%%piper

sample_sales()
>> select(['-target_profit', '-month'])
>> str_join(columns=['actual_sales', 'product', 'actual_profit'],
            sep='|',
            column='actual_sales',
            drop=False)
>> head(tablefmt='plain')

     location product   target_sales actual_sales actual_sales_              actual_profit
  4  London   Beachwear        31749        29209 29209.08|Beachwear|1752.54          1753
125  London   Beachwear        37833        34050 34049.7|Beachwear|5447.95           5448
 21  London   Jeans            29485        31549 31548.95|Jeans|4416.85              4417
148  London   Jeans            37524        40901 40901.16|Jeans|4090.12              4090