我需要帮助应用我下面的情况下的语句函数到同一列一次或并行?不确定我是否以最有效的方式做这件事,还有其他方法吗?
#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)
不确定将所有功能放在一个大函数下是否是个好主意?
应该有一个函数来决定调用哪个函数。将该函数应用于数据框架。根据您的条件,该函数可以调用包含计算内容的正确函数。此外,为了提高可读性,请将函数和变量重命名为有意义的名称:
#Accrued Calc for ACT/360
def accrued_act_360(row):
if row['Type'] == 'L' and row['Current Filter'] == 'Current CF':
return 1 * row['Principal/GrossAmount'] * (row['All in Rate']/100)* (row['Number of days'])/360
elif row['Type'] == 'D':
return -1 * row['Principal/GrossAmount'] * (row['All in Rate']/100)* (row['Number of days'])/360
else:
return ''
#Accrued Calc for ACT/365
def accrued_act_365(row):
if row['Type'] == 'L' and row['Current Filter'] == 'Current CF':
return 1 * row['Principal/GrossAmount'] * (row['All in Rate']/100)* (row['Number of days'])/365
elif row['Type'] == 'D':
return -1 * row['Principal/GrossAmount'] * (row['All in Rate']/100)* (row['Number of days'])/365
else:
return ''
#Accrued Calc for 30/360 Basis
def accrued_30_360(row):
if row['Type'] == 'L' and row['Current Filter'] == 'Current CF':
return 1 * row['Principal/GrossAmount'] * (row['All in Rate']/100)* (360 *(Settlement.year - row['Start Date YEAR']) + 30 * (Settlement.month - row['Start Date MONTH']) + Settlement.day - row['Start Date DAYS'])/360
elif row['Type'] == 'D':
return -1 * row['Principal/GrossAmount'] * (row['All in Rate']/100)* (360 *(Settlement.year - row['Start Date YEAR']) + 30 * (Settlement.month - row['Start Date MONTH']) + Settlement.day - row['Start Date DAYS'])/360
else:
return ''
def accrued_calc(row):
if row["Basis"] == "ACT/360":
return accrued_act_360(row)
elif row["Basis"] == "ACT/365":
return accrued_act_365(row)
elif row["Basis"] == "30/360":
return accrued_30_360(row)
else:
return ""
kf['Accrued Calc'] = kf.apply(accrued_calc, axis = 1)
:这种方法无法利用熊猫惊人的矢量化处理能力。
您可以使用布尔索引来确定哪些行满足某些条件,并且一次只为整个数据框设置这些行,而不是逐行应用您的函数。这种方法可能比.apply
def accrued_act_360_vec(df):
# Find which rows match your condition
type_l_rows = (df["Basis"] == "ACT/360") & (df["Type"] == "L") & (df["Current Filter"] == "Current CF")
# Set the value for those rows
df.loc[type_l_rows, "Accrued Calc"] = df.loc[type_l_rows, 'Principal/GrossAmount'] * (df.loc[type_l_rows, 'All in Rate']/100)* (df.loc[type_l_rows, 'Number of days'])/360
type_d_rows = (df["Basis"] == "ACT/360") & (df["Type"] == "D")
df.loc[type_d_rows, "Accrued Calc"] = -1 * df.loc[type_d_rows, 'Principal/GrossAmount'] * (df.loc[type_d_rows, 'All in Rate']/100)* (df.loc[type_d_rows, 'Number of days'])/360
# No need to consider the else condition: Those rows never get set.
def accrued_act_365_vec(df):
type_l_rows = (df["Basis"] == "ACT/365") & (df['Type'] == 'L') & (df['Current Filter'] == 'Current CF')
df.loc[type_l_rows, "Accrued Calc"] = 1 * df.loc[type_l_rows, 'Principal/GrossAmount'] * (df.loc[type_l_rows, 'All in Rate']/100)* (df.loc[type_l_rows, 'Number of days'])/365
type_d_rows = (df["Basis"] == "ACT/365") & (df['Type'] == 'D')
df.loc[type_d_rows, "Accrued Calc"] = -1 * df.loc[type_d_rows, 'Principal/GrossAmount'] * (df.loc[type_d_rows, 'All in Rate']/100)* (df.loc[type_d_rows, 'Number of days'])/365
def accrued_30_360_vec(df):
type_l_rows = (df["Basis"] == "30/360") & (df['Type'] == 'L') & (df['Current Filter'] == 'Current CF')
df.loc[type_l_rows, "Accrued Calc"] = 1 * df.loc[type_l_rows, 'Principal/GrossAmount'] * (df.loc[type_l_rows, 'All in Rate']/100)* (360 *(Settlement.year - df.loc[type_l_rows, 'Start Date YEAR']) + 30 * (Settlement.month - df.loc[type_l_rows, 'Start Date MONTH']) + Settlement.day - df.loc[type_l_rows, 'Start Date DAYS'])/360
type_d_rows = (df["Basis"] == "30/360") & (df['Type'] == 'D')
df.loc[type_d_rows, "Accrued Calc"] = -1 * df.loc[type_d_rows, 'Principal/GrossAmount'] * (df.loc[type_d_rows, 'All in Rate']/100)* (360 *(Settlement.year - df.loc[type_d_rows, 'Start Date YEAR']) + 30 * (Settlement.month - df.loc[type_d_rows, 'Start Date MONTH']) + Settlement.day - df.loc[type_d_rows, 'Start Date DAYS'])/360
注意这些函数包括df["Basis"] == ...
的条件,因为它们都是独立的函数。要运行这些,只需执行:
accrued_act_360_vec(kf)
accrued_act_365_vec(kf)
accrued_30_360_vec(kf)
请重新检查我代码中公式的准确性,我可能在复制/粘贴时不小心把它们弄乱了