我试图在pandas数据框中创建一个计算列,该列根据数据框中的另一列运行不同的计算
首先,我尝试:
df_rollup['modeled_days'] = abs(round(((df_rollup.risk_avg) - 31) / (master_weight /100) / (prod_tolerance / 100), 0)).where(df_rollup['completion_status'] == 'PRODUCING')
df_rollup['modeled_days'] = abs(round(((df_rollup.risk_avg) - 31) / (master_weight / 100) / (shutin_tolerance / 100), 0)).where(df_rollup['completion_status'] == 'SHUT IN')
df_rollup['modeled_days'] = abs(round(((df_rollup.risk_avg) - 31) / (master_weight / 100) / (abandoned_tolerance / 100), 0)).where(df_rollup['completion_status'].str.contains('ABANDONED'))
我很快意识到,这将用最后的更新计算覆盖每一行,并将不匹配的行替换为Nan
所以我研究了另一种方法,我认为这是正确的,但我收到了错误:ValueError:序列的真值是模糊的。使用a.empty、a.bool()、a.item()、a.any()或a.all()
这是我的方法:
def production_type_calc(df_rollup, master_weight, prod_tolerance, shutin_tolerance, abandoned_tolerance):
if df_rollup['completion_status'] == 'PRODUCING':
return abs(round((df_rollup.risk_avg - 31) / (master_weight / 100) / (prod_tolerance / 100), 0))
elif df_rollup['completion_status'] == 'SHUT IN':
return abs(round((df_rollup.risk_avg - 31) / (master_weight / 100) / (shutin_tolerance / 100), 0))
elif df_rollup['completion_status'].str.contains('ABANDONED'):
return abs(round((df_rollup.risk_avg - 31) / (master_weight / 100) / (abandoned_tolerance / 100), 0))
else:
return 0
我使用此.apply方法运行此函数,如下所示:
df_rollup['modeled_days'] = df_rollup.apply(production_type_calc(df_rollup, master_weight, prod_tolerance, shutin_tolerance, abandoned_tolerance), axis=1)
我以前遇到过这个问题,似乎我需要嵌套数据帧,例如df=df[df['']或类似的东西,但我不知道如何开始。我希望在这方面得到任何帮助
我仍然喜欢你的第一种方法,我们可以做
np.select
相关问题 更多 >
编程相关推荐