我需要帮助将下面的case语句函数一次或并行应用到同一列?不确定是否以最有效的方式进行,是否有其他方法可以实现?
#Accrued Calc for ACT/360
def bbb(bb):
if bb["Basis"] == "ACT/360" and bb['Type'] == 'L' and bb['Current Filter'] == 'Current CF':
return 1 * bb['Principal/GrossAmount'] * (bb['All in Rate']/100)* (bb['Number of days'])/360
elif bb["Basis"] == "ACT/360" and bb['Type'] == 'D':
return -1 * bb['Principal/GrossAmount'] * (bb['All in Rate']/100)* (bb['Number of days'])/360
else:
return ''
kf['Accrued Calc'] = kf.apply(bbb, axis = 1)
#Accrued Calc for ACT/365
def ccc(cc):
if cc["Basis"] == "ACT/365" and cc['Type'] == 'L' and cc['Current Filter'] == 'Current CF':
return 1 * cc['Principal/GrossAmount'] * (cc['All in Rate']/100)* (cc['Number of days'])/365
elif cc["Basis"] == "ACT/365" and cc['Type'] == 'D':
return -1 * cc['Principal/GrossAmount'] * (cc['All in Rate']/100)* (cc['Number of days'])/365
else:
return ''
kf['Accrued Calc'] = kf.apply(ccc, axis = 1)
#Accrued Calc for 30/360 Basis
{def ppp(ll):
if ll["Basis"] == "30/360" and ll['Type'] == 'L' and ll['Current Filter'] == 'Current CF':
return 1 * ll['Principal/GrossAmount'] * (ll['All in Rate']/100)* (360 *(Settlement.year - ll['Start Date YEAR']) + 30 * (Settlement.month - ll['Start Date MONTH']) + Settlement.day - ll['Start Date DAYS'])/360
elif ll["Basis"] == "30/360" and ll['Type'] == 'D':
return -1 * ll['Principal/GrossAmount'] * (ll['All in Rate']/100)* (360 *(Settlement.year - ll['Start Date YEAR']) + 30 * (Settlement.month - ll['Start Date MONTH']) + Settlement.day - ll['Start Date DAYS'])/360
else:
return ''
kf['Accrued Calc'] = kf.apply(ppp, axis = 1)}
我试过下面的
kf['Accrued Calc'] = kf['Accrued Calc'].apply(bbb) & kf['Accrued Calc'].apply(ccc) & kf['Accrued Calc'].apply(ppp)
不确定将所有函数都放在一个大函数下是否合适?
1条答案
按热度按时间ffdz8vbo1#
你应该有一个函数来决定要调用哪个函数。将该函数应用到你的 Dataframe 中。根据你的条件,这个函数可以调用正确的函数,其中包含你的计算内容。另外,为了可读性,请将你的函数和变量重命名为有意义的名称:
**然而:**这种方法无法利用Pandas惊人的矢量化处理能力。
您可以使用布尔索引来确定哪些行满足特定条件,并且只为 * 整个 Dataframe * 一次性设置这些行,而不是逐行应用函数。
请注意,这些函数包含
df["Basis"] == ...
的条件,因为它们都是独立函数。要运行这些函数,只需执行以下操作:请重新检查我的代码中公式的准确性,我可能在复制/粘贴过程中不小心把它们搞砸了