Skip to content

Allow DataFrame.apply() to accept dict of funcs #14464

Closed
@mattayes

Description

@mattayes

When using DataFrame.apply(), it would be helpful to be able to apply different functions to different columns. There's a nice workaround on StackOverflow:

import numpy as np
import pandas as pd

def multi_func(functions):
    def f(col):
        return functions[col.name](col)
    return f

df = pd.DataFrame(np.random.random((10, 2)), columns=['A', 'B'])
results = df.apply(multi_func({'A': np.mean, 'B': np.sum}))

results
A    0.401456
B    6.845529
dtype: float64

My guess is that the changes would be made in pandas.core.frame.DataFrame, potentially adding a new _apply_X method. Any thoughts/advice?

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions