了解 Pandas 中的 apply 和 groupby

Understanding apply and groupby in Pandas

我正在尝试理解 Python Wes McKinney 的数据分析书中的一个示例。我查看了 pandas 食谱、文档和 SO,但找不到这样的示例。

该示例查看 2012 年联邦选举委员会数据库 (https://github.com/wesm/pydata-book/blob/master/ch09.ipynb)。下面的代码确定了向奥巴马和罗姆尼捐款最多的职业。

我很难理解该函数如何获取一个 groupby 对象并对其执行另一个 groupby 操作。当我 运行 this 在函数之外时,我得到一个错误。有人可以阐明这种行为吗?

谢谢,

伊万

# top donor occupations donating to Obama or Romney
def get_top_amounts(group, key, n = 5):
    totals = group.groupby(key)['contb_receipt_amt'].sum()

    return totals.sort_values(ascending  = False)[:n]

# first group by candidate
grouped = fec_mrbo.groupby('cand_nm')

# for each group, group again by contb_receipt_amt so we have a hierarchical index
# take the contribution amount
# then return the total amount for each occupation by cand sorted to give top n
grouped.apply(get_top_amounts, 'contbr_occupation', n= 5)

结果是这样的

cand_nm        contbr_occupation                     
Obama, Barack  RETIRED                                   25270507.23
               ATTORNEY                                  11126932.97
               INFORMATION REQUESTED                      4849801.96
               HOMEMAKER                                  4243394.30
               PHYSICIAN                                  3732387.44
               LAWYER                                     3159391.87
               CONSULTANT                                 2459812.71
Romney, Mitt   RETIRED                                   11266949.23
               INFORMATION REQUESTED PER BEST EFFORTS    11173374.84
               HOMEMAKER                                  8037250.86
               ATTORNEY                                   5302578.82
               PRESIDENT                                  2403439.77
               EXECUTIVE                                  2230653.79
               C.E.O.                                     1893931.11

当您在分组数据框上使用 apply 时,您实际上是在遍历组并将函数传递给每个组,您正在应用。 让我们看一个简单的例子:

import pandas as pd
df = pd.DataFrame({'col1': [1,1,1,1,2,2,2,2],
                   'col2': ['a','b','a','b','a','b','a','b'],
                   'value': [1,2,3,4,5,6,7,8]})
grouped = df.groupby('col1')

现在让我们创建一个简单的函数,它允许我们查看传递给函数的内容:

def print_group(group):
    print(group)
    print('=' * 10)

grouped.apply(print_group)
   col1 col2  value
0     1    a      1
1     1    b      2
2     1    a      3
3     1    b      4
==========
   col1 col2  value
0     1    a      1
1     1    b      2
2     1    a      3
3     1    b      4
==========
   col1 col2  value
4     2    a      5
5     2    b      6
6     2    a      7
7     2    b      8
==========

如您所见,每个组都作为单独的数据帧传递给函数。当然,您可以将所有正常功能应用于此子集。 您看到第一组两次是内部原因,无法更改,这不是错误 ;)。

让我们创建另一个函数来证明这一点:

def second_group_sum(group):
    res = group.groupby('col2').value.sum()
    print(res)
    print('=' * 10)
    return res

grouped.apply(second_group_sum)
col2
a    4
b    6
Name: value, dtype: int64
==========
col2
a    4
b    6
Name: value, dtype: int64
==========
col2
a    12
b    14
Name: value, dtype: int64
==========

你甚至可以更进一步,做 group-apply-group-apply-group-apply 等等......

我希望这有助于理解正在发生的事情。

顺便说一下,如果你使用 ipdb(调试工具),你可以在应用函数中设置一个断点,与组数据帧进行交互。