Skip to content

A map method to work with many columns #20807

@oricou

Description

@oricou

Hi,

I would like to map a function expecting N arguments to a DataFrame of N columns. Today to do that I do:

df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [5, 6, 7, 8]})
def f(x,y):
     return x*y

df[['A', 'B']].apply(lambda x: f(*x), axis=1) 

So the map method for DataFrame would be:

def map(self, f):
    return self.apply(lambda x: f(*x), axis=1)

if I am right. Then df.map(f) would produce a Serie.

Note that with 1 colummn and a 1 argument function map would work as numpy.map does.

Thanks,

Olivier.

ps: I edited this post after the talk I had with Tom, so it must seem strange now 😃

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions