简体   繁体   English

熊猫中的Lambda函数表示法

[英]Lambda function notation in Pandas

I received a wonderful lambda function from a user a while ago. 前一段时间,我从用户那里收到了一个很棒的lambda函数。

actresses_modified['Winner_Count'] = actresses_modified.apply(lambda x: actresses_modified.Name.value_counts()[x.Name], axis=1)

The data frame to which it is applied looks like this: 将其应用于的数据帧如下所示:

    Year    Award           Winner  Name
2   1928    Best Actress    0.0     Louise Dresser
3   1928    Best Actress    1.0     Janet Gaynor
4   1928    Best Actress    0.0     Gloria Swanson
40  1929    Best Actress    0.0     Ruth Chatterton
41  1929    Best Actress    0.0     Betty Compson

The problem is I have forgotten how it works (I had to step away from this "for fun" project) and, more specifically, exactly what is going on with [x.Name] . 问题是我忘记了它是如何工作的(我不得不离开这个“取乐”项目),更具体地说,正是[x.Name]发生了什么。

The line actresses_modified.Name.value_counts() by itself gives me the count of all actress names in the data frame. actresses_modified.Name.value_counts()本身给了我数据框中所有女演员的人数。 What does [x.Name] mean in english, how does it manage to tally up all of the 1s next to each person's name in the data frame's Winner column, and return a correct tally of the total number of wins? [x.Name]在英语中是什么意思,它如何设法对数据框的“获胜者”列中每个人的名字旁边的所有1进行汇总,并返回正确的总获胜次数? Of equal importance, does this type of syntax have a name? 同样重要的是,这种语法是否有名称? My google searches turned up nada. 我的Google搜索结果显示为nada。

Any thoughts would be appreciated? 任何想法将不胜感激?

Here, I'm not sure I made myself clear in the comment. 在这里,我不确定我是否在评论中表达了自己的意思。 So the apply method "Applies function along input axis of DataFrame." 因此, apply方法“沿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: 因此,为简单起见,我们假设有一个Actress对象集合,称为actresses_modified,它看起来像这样:

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

Let's assume that this is how the Actress is defined: 让我们假设这是Actress的定义方式:

class Actress:
    Name = "Some String"

So then we have our lambda function which gets applied to each actress in the collection as x . 因此,我们有了lambda函数,该函数将作为x应用于集合中的每个女演员。 value_counts() returns "object containing counts of unique values." value_counts()返回“包含唯一值计数的对象”。

So when we call value_counts() for each actress we're getting that Actress's counts value by key. 因此,当我们为每个女演员调用value_counts() ,我们将获得女演员的按值计数。 Let's pretend that value_counts() returns a dict with actress names and their "count" and it looks like this: 让我们假设value_counts()返回一个包含女演员名称及其“ count”的字典,它看起来像这样:

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. 而且我们的Actress对象的女演员1的Name是“ Jane Doe”,因此当我们调用value_counts()[x.Name]我们正在做value_counts()[x.Name] counts["Jane Doe"] ,该返回值是1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM