janitor.join_apply

janitor.join_apply(df: pandas.core.frame.DataFrame, func: Callable, new_column_name: str) → pandas.core.frame.DataFrame[source]

Join the result of applying a function across dataframe rows.

This method does not mutate the original DataFrame.

This is a convenience function that allows us to apply arbitrary functions that take any combination of information from any of the columns. The only requirement is that the function signature takes in a row from the DataFrame.

The example below shows us how to sum the result of two columns into a new column.

df = (
    pd.DataFrame({'a':[1, 2, 3], 'b': [2, 3, 4]})
    .join_apply(lambda x: 2 * x['a'] + x['b'], new_column_name="2a+b")
)

This following example shows us how to use conditionals in the same function.

def take_a_if_even(x):
    if x['a'] % 2:
        return x['a']
    else:
        return x['b']

df = (
    pd.DataFrame({'a': [1, 2, 3], 'b': [2, 3, 4]})
    .join_apply(take_a_if_even, 'a_if_even')
)
Parameters
  • df – A pandas DataFrame

  • func – A function that is applied elementwise across all rows of the DataFrame.

  • new_column_name – New column name.

Returns

A pandas DataFrame with new column appended.