This question has a big chance to be duplicated but I haven't found an answer yet. However, I'm trying to apply a function to a pandas DataFrame and I want to have a DataFrame back. Followed example is reproducible:
df = pd.DataFrame({'ID': ["1","2"],
'Start': datetime.strptime('20160701', '%Y%m%d'),
'End': datetime.strptime('20170701', '%Y%m%d'),
'Value': [100, 200],
'CreditNote': [-20, -30]})
My function:
def act_value_calc(x):
start_delta = (x.Start.replace(day=31,month=12) - x.Start).days
full_delta = (x.End - x.Start).days
result1 = round( (x.Value + x.CreditNote) / full_delta * start_delta, 2)
result2 = round( (x.Value + x.CreditNote) - result1, 2)
return(pd.DataFrame({'r1': [result1],'r2': [result2]}))
Why I can not run the following code ...
df.apply(act_value_calc, 1)
and what should be done to let it run? I mean to get a DataFrame or a list back with result1 and result2?