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
  Python openyxl not updating Excel file MrBean12 1 336 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 442 Feb-07-2024, 12:24 PM
Last Post: Viento
  Recommended way to read/create PDF file? Winfried 3 2,895 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,455 Nov-09-2023, 10:56 AM
Last Post: mg24
  Search Excel File with a list of values huzzug 4 1,248 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 842 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  read file txt on my pc to telegram bot api Tupa 0 1,127 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,115 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,287 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,630 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

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