如何通过apply函数调试pandas group

问题描述 投票:0回答:1

我正在努力理解我的前同事所写的功能。

def generate_df(group):  
    date_str = str(group['CallerLocal_Date'].iloc[-1]) + ' {0}:00:00'
    # some other functions
    return something

enrich_df = df.groupby(['LeadNumber', 'CallerLocal_Date'], as_index=False).apply(generate_df).reset_index(drop=True)

我无法完全理解上面的功能所以我试图组合并实际看到date_str = str(group['CallerLocal_Date'].iloc[-1]) + ' {0}:00:00'线的作用。

df看起来像这样

        LeadNumber             CallerLocal_Date  Caller_TimeZone
0      7-OH4UMXXL5                   2017-09-13  America/Chicago
1      7-OL4ZHUF47                   2017-09-26  America/Chicago
2      7-OL4ZHUF47                   2017-09-26  America/Chicago
3      7-OHMFNFFC2                   2017-09-13  America/Chicago
4      7-OHMFNFFC2                   2017-09-12  America/Chicago
5      7-OGBMIPIIN                   2017-09-11  America/Chicago
6      7-OGBMIPIIN                   2017-09-07  America/Chicago
7      7-OETJOA7O6                   2017-09-01  America/Chicago
8      7-OETJOA7O6                   2017-09-06  America/Chicago
9      7-OILTU4T5O                   2017-09-18  America/Chicago
10     7-OGJHKCJFZ                   2017-09-07  America/Chicago

所以我定义了

group = df.groupby(['LeadNumber', 'CallerLocal_Date'], as_index=False)

并打电话

date_str = str(group['CallerLocal_Date'].iloc[-1]) + ' {0}:00:00'

然后我得到了

AttributeError: Cannot access callable attribute 'iloc' of 'DataFrameGroupBy' objects, try using the 'apply' method

有人能指点我如何调试groupby对象,而不使用apply函数?

pandas pandas-groupby pandas-apply
1个回答
1
投票

你可以做:

groups = df.groupby(['LeadNumber', 'CallerLocal_Date'], as_index=False)
group = groups.get_group(list(groups.groups)[0])

然后你可以逐行运行你的代码:

date_str = str(group['CallerLocal_Date'].iloc[-1]) + ' {0}:00:00'
© www.soinside.com 2019 - 2024. All rights reserved.