将多个函数应用于大熊猫groupby应用是否返回多个数据帧?

时间:2018-12-27 07:10:02

标签: python pandas

目前,我正在按三个groupby进行此操作,每个应用都返回一个数据帧:

 fd1 = df.groupby('loan_id').apply(build_feature_set_1)
 fd2 = df.groupby('loan_id').apply(build_feature_set_2)
 fd3 = df.groupby('loan_id').apply(build_feature_set_3)

这些功能需要访问多个列,因此我不能使用agg。 本质上,我希望能够通过一次数据扫描(即单个groupby)来做到这一点。有可能吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试这种方法:-

fn_list = [build_feature_set_1, build_feature_set_2, build_feature_set_3]

grouped_df = df.groupby('loan_id')

df1, df2, df3 = list(map(lambda x: grouped_df.apply(x), fn_list))