I am trying to create a function which creates new variables in a DataFrame based on multi-level variable grouping. The below patch satisfactorily produce the desired result, however the execution time is on the higher side for a run-time environment.
Can anyone suggest a better alternative ?
def prob_calc(test1):
test1["log_sum"] = np.log(sum(np.exp(test1.utility)))
test1["prob_logit_within_nest"] = np.exp(test1.utility)/sum(np.exp(test1.utility))
test1["Freq_of_nest"] = sum(test1.flag)
return test1
test1 = test1.groupby(['quest_number','task','nest']).apply(prob_calc)
Thanks, Sombit