Pandas 如何通过应用函数调试组件

Pandas 如何通过应用函数调试组件,pandas,pandas-groupby,pandas-apply,Pandas,Pandas Groupby,Pandas Apply,我试图理解我的前同事写的函数 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_

我试图理解我的前同事写的函数

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)
我无法完全理解上面的函数,所以我尝试按groupby并实际查看
date\u str=str(group['CallerLocal\u date'].iloc[-1])+'{0}:00:00'
line的功能

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
有人能告诉我如何在不使用
apply
函数的情况下调试groupby对象吗?

您可以执行以下操作:

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'

在generate_df函数
print(type(group))
print(group)
print('\n')
)中添加三个print语句,这将让您准确地看到传递的内容。感觉
pandas
应该有一种更优雅的方式来完成这一操作。可以是类似于
df.groupby(..,debug=True,…)