Skip to content Skip to sidebar Skip to footer

Lambda Function Notation In Pandas

I received a wonderful lambda function from a user a while ago. actresses_modified['Winner_Count'] = actresses_modified.apply(lambda x: actresses_modified.Name.value_counts()[x.Nam

Solution 1:

Here, I'm not sure I made myself clear in the comment. So the apply method "Applies function along input axis of DataFrame." So let's say, for simplicity's sake, that we have a collection of Actress objects called actresses_modified and it looks like this:

   actresses_modified = [<Actress>, <Actress>, <Actress>, <Actress>]

Let's assume that this is how the Actress is defined:

classActress:
    Name = "Some String"

So then we have our lambda function which gets applied to each actress in the collection as x. value_counts() returns "object containing counts of unique values."

So when we call value_counts() for each actress we're getting that Actress's counts value by key. Let's pretend that value_counts() returns a dict with actress names and their "count" and it looks like this:

counts = {
    'Jane Doe': 1,
    'Betty Ross': 3,
}

And we have our Actress objects with actress 1's Name is "Jane Doe", so when we call value_counts()[x.Name] we're doing counts["Jane Doe"] which would return 1.

Post a Comment for "Lambda Function Notation In Pandas"