I have to calculate the value of S, which has the formula as: S = (25400/CN) − 254
the CN value which I have to choose will depend on the amc_active condition viz 1, 2 and 3. if amc_active condition at 'index 0 or 1st row' is 1 then I have to choose the CN value from column cn1 i.e 47
and if the amc_active is 3, then I have to choose CN value as 95 from cn3 column in the 4th row and so on..
cn1 cn2 cn3 amc_active
0 47 56 78 1
1 55 61 87 2
2 36 67 73 1
3 42 84 95 3
... ... ... ... ... ... ... ... ...
17410 42 84 96 3
17411 48 81 85 1
17412 55 59 82 1
17413 57 86 93 2
17414 36 87 91 2
for doing so, I am using else if condition
if (df_col_all_merged['amc_active'] == 1):
cn_for_s = df_col_all_merged['cn1']
elif (df_col_all_merged['amc_active'] == 2):
cn_for_s = df_col_all_merged['cn2']
elif (df_col_all_merged['amc_active'] == 3):
cn_for_s = df_col_all_merged['cn3']
but getting the error as
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-43-40c3b3817474> in <module>
----> 1 if (df_col_all_merged['amc_active'] == 1):
2 cn_for_s = df_col_all_merged['cn1']
3 elif (df_col_all_merged['amc_active'] == 2):
4 cn_for_s = df_col_all_merged['cn2']
5 elif (df_col_all_merged['amc_active'] == 3):
~\Anaconda3\envs\geocube\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1327
1328 def __nonzero__(self):
-> 1329 raise ValueError(
1330 f"The truth value of a {type(self).__name__} is ambiguous. "
1331 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
and from this
def select_cn(df_col_all_merged):
result = cn_for_s
if (df_col_all_merged['amc_active'] == 1):
cn_for_s = df_col_all_merged['cn1']
elif (df_col_all_merged['amc_active'] == 2):
cn_for_s = df_col_all_merged['cn2']
elif (df_col_all_merged['amc_active'] == 3):
cn_for_s = df_col_all_merged['cn3']
return result
df_col_all_merged['s_mm'] = (25400/select_cn(df_col_all_merged)) - 254
the error is
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-54-df43eddeac39> in <module>
----> 1 df_col_all_merged['s_mm'] = (num_const/select_cn(df_col_all_merged)) - dev_const
<ipython-input-51-7405a6dd24db> in select_cn(df_col_all_merged)
1 def select_cn(df_col_all_merged):
----> 2 result = cn_for_s
3 if (df_col_all_merged['amc_active'] == 1):
4 cn_for_s = df_col_all_merged['cn1']
5 elif (df_col_all_merged['amc_active'] == 2):
UnboundLocalError: local variable 'cn_for_s' referenced before assignment
How to rectify this?