I want only one value column as a result in below code:
df = pd.DataFrame({'team':['a','a'],'balance':[100,3],'dpd':[0,60]})
df.pivot_table(index='team',values=['balance','dpd'],
aggfunc=lambda x: np.sum(np.where(x.dpd>=30,x.balance,0))/np.sum(x.balance))
this return:
balance dpd
team
a 0.029126 0.029126
But, what I want is a column with new name :
dqratio
team
a 0.029126
I think you are looking for groupby
and apply
df.groupby('team').apply(lambda x: np.sum(np.where(x['dpd']>=30,x['balance'],0))/np.sum(x['balance'])).to_frame('dqratio')
dqratio
team
a 0.029126
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With