简体   繁体   中英

Apply multiple functions to a pandas groupby apply that returns multiple dataframes?

Current I am doing it in three groupby's with each apply returning a dataframe:

 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)

These functions require access to multiple columns so I can't use agg . Essentially I want to be able to do it in a single scan of data ie a single groupby. Is it possible?

You can try this approach:-

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM