Python Forum
Read excel file to determine the rules - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Read excel file to determine the rules (/thread-26730.html)



Read excel file to determine the rules - abc12345 - May-11-2020

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.


RE: Read excel file to determine the rules - Larz60+ - May-11-2020

What have you tried?


RE: Read excel file to determine the rules - abc12345 - May-12-2020

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.


RE: Read excel file to determine the rules - abc12345 - May-12-2020

(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.



RE: Read excel file to determine the rules - abc12345 - May-13-2020

Any help/pointers would be much appreciated.

Thanks.