Python Forum
Read excel file to determine the rules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read excel file to determine the rules
#1
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.
Reply
#2
What have you tried?
Reply
#3
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.
Reply
#4
(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.
Reply
#5
Any help/pointers would be much appreciated.

Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,188 Dec-19-2024, 11:57 AM
Last Post: snippsat
  python read PDF Statement and write it into excel mg24 1 1,016 Sep-22-2024, 11:42 AM
Last Post: Pedroski55
  Read TXT file in Pandas and save to Parquet zinho 2 1,303 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 1,631 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,940 Jul-19-2024, 06:42 PM
Last Post: snippsat
  docx file to pandas dataframe/excel iitip92 1 2,705 Jun-27-2024, 05:28 AM
Last Post: Pedroski55
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,373 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Python openyxl not updating Excel file MrBean12 1 2,111 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 1,601 Feb-07-2024, 12:24 PM
Last Post: Viento
  Recommended way to read/create PDF file? Winfried 3 5,012 Nov-26-2023, 07:51 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020