我试图理解我的前同事所写的功能。
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
函数吗?
答案 0 :(得分: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'