我下面列出了一个pd df。
data={'Equipment_Class':['PC1', 'PC1', 'PC1', 'PC2', 'PC2', 'PC2','PC2'],
'EL_base':['0','1','2','0','1','2','3'],
'Installed Price': ['235.67', '236.50', '245.81', '506.66',
'506.66','507.34','516.78'],
'First Year Operating Cost':
['1.62','0.85','0.16','2.27','1.27','1.29','0.34'],
'LCC Savings':['0.00','0.00','-3.27','0.00','7.92','7.48','5.20']}
df=pd.DataFrame(data=data,columns=['Equipment_Class','EL_base','Installed Price',\
'First Year Operating Cost','LCC Savings'])
我想在“ LCC储蓄”列之后添加另一列“简单PBP”。
新列的计算方法为:
if "LCC savings" is 0, 0,
else ("Installed Price"- EL_base(0) Installed Price)/(EL_base(0)"First Year
Operating Cost"-"First Year Operating Cost"
例如:
PC1, EL_base =2
Simple PBP =(245.81-235.67)/(1.62-0.16)
此计算方法适用于每个设备组。 我该怎么办?
答案 0 :(得分:1)
那又怎么样:
#Cast to float
df[['Installed Price','First Year Operating Cost', 'LCC Savings']] = df[['Installed Price','First Year Operating Cost','LCC Savings']].astype(float)
# Get first prices out:
df[['install_0', 'fyoc0']] = df.groupby('Equipment_Class')[['Installed Price','First Year Operating Cost']].transform('first')
# Run calc
result = (df['Installed Price']-df['install_0'])/(df['First Year Operating Cost']-df['fyoc0'] )
# Mask rows to 0:
df['Simple PBP'] = result.mask(df["LCC Savings"]==0, 0)
编辑:错过了每个组的要求!更改答案。
答案 1 :(得分:1)
您需要将数据框中的数字转换为...数字:
df['EL_base'] = df['EL_base'].astype('int')
df[['Installed Price', 'First Year Operating Cost', 'LCC Savings']] = df[['Installed Price', 'First Year Operating Cost', 'LCC Savings']].astype(float)
计算:
def calcualte_pbp(group):
base = group[group['EL_base'] == 0].iloc[0]
installed_price = group['Installed Price'] - base['Installed Price']
fyoc = base['First Year Operating Cost'] - group['First Year Operating Cost']
return group.assign(**{'Simple PBP': np.where(group['LCC Savings'] == 0, 0, installed_price / fyoc)})
df.groupby('Equipment_Class').apply(calcualte_pbp).reset_index(drop=True)
结果:
Equipment_Class EL_base Installed Price First Year Operating Cost LCC Savings Simple PBP
0 PC1 0 235.67 1.62 0.00 0.000000
1 PC1 1 236.50 0.85 0.00 0.000000
2 PC1 2 245.81 0.16 -3.27 6.945205
3 PC2 0 506.66 2.27 0.00 0.000000
4 PC2 1 506.66 1.27 7.92 0.000000
5 PC2 2 507.34 1.29 7.48 0.693878
6 PC2 3 516.78 0.34 5.20 5.243523