Python Forum

Full Version: Read excel file to determine the rules
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I have an excel file with user defined business rules as below,

Column_Name Operator Column_Value1 Column_Value2 Operand RuleID Result
ABC Equal 12 and 1 1
CDE Equal 10 and 1 1
XYZ Equal AD 1 1.5
ABC Equal 11 and 2 1
CDE Equal 10 2 1.2
and so on.

Input file (CSV) will look like below,
ABC,CDE,XYZ
12,10,AD
11,10,AD

Goal here is to derive an output column called Result which needs to be looked up to the user defined business rule excel.

O/p Expected
ABC,CDE,XYZ,Result
12,10,AD,1.5
11,10,AD,1.2

Thanks in advance.
What have you tried?
Hi,

I did try to convert the user defined business rule (excel) to an IF condition statement and trying to assign the entire If elif statements to an function. So that I can pass it to below statement to apply the rules.

ouput_df['der_factor'] = input_df.apply(result_func, axis = 1)

When I have the function with manually coding the rules it works (as shown below), I need help on couple of things,
1. Is IF statement the efficient way to handle this scenario?
2. If so, how can I pass the entire dynamically generated IF statement (including elif and returns) in the function.

def result_func(input_df):

if (input_df['ABC'] == 12):
return '1.25'
elif (ip_df['ABC'] == 11):
return '0.25'
else:
return '1'

Any pointers/suggestions would be much helpful.
Thanks.
(May-12-2020, 01:46 PM)abc12345 Wrote: [ -> ]Hi,

I did try to convert the user defined business rule (excel) to an IF condition statement and trying to assign the entire If elif statements to an function. So that I can pass it to below statement to apply the rules.

 ouput_df['der_factor'] = input_df.apply(result_func, axis = 1) 
When I have the function with manually coding the rules it works (as shown below), I need help on couple of things,
1. Is IF statement the efficient way to handle this scenario?
2. If so, how can I pass the entire dynamically generated IF statement (including elif and returns) in the function.
def result_func(input_df):

    if (input_df['ABC'] == 12):
        return '1.25'
    elif (ip_df['ABC'] == 11):
        return '0.25'
    else:
        return '1'
Any pointers/suggestions would be much helpful.
Thanks.
Any help/pointers would be much appreciated.

Thanks.