Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
logic python
#1
Hey
I have this excel code i try to translate into python. I am getting some wrong answers and i need help to figure it out..
I have a dataset where I try ti check if a value is bigger or lower than two other values.

 
#Dataset
Date    STD-3   STD-25  STD-2   STD-15  STD-1   Data    STD1    STD15   STD2    STD25   STD3
11.05.2022  -0,057406797    -0,047838998    -0,038271198    -0,028703399    -0,019135599    0,021233631 0,019135599 0,028703399 0,038271198 0,047838998 0,057406797

#Excel Code: 

"Data" < "STD1" and "Data" > "STD-1" = 0

"Data" > "STD1" and "Data" < "STD15" = 1

"Data" > "STD15" and "Data" < "STD2" = 1,5

"Data" > "STD2" and "Data" < "STD25" = 2

"Data" > "STD25" and "Data" < "STD3" = 2,5

"Data" > "STD3" = 3

"Data" < "STD-1" and "Data" > "STD-15" = -1

"Data" < "STD-15" and "Data" > "STD-2" = -1,5

"Data" < "STD-2" and "Data" > "STD-25" = -2

"Data" < "STD-25" and "Data" > "STD-3" = -2,5

"Data" > "STD3" = -3

#Python Code: 

condition = [((df['Data'] < df['STD1']) & (df['Data'] > df['STD-1'])), 
             ((df['Data'] > df['STD1']) & (df['Data'] < df['STD15'])), 
             ((df['Data'] > df['STD15']) & (df['Data'] < df['STD2'])),
             ((df['Data'] > df['STD2']) & (df['Data'] < df['STD25'])), 
             ((df['Data'] > df['STD25']) & (df['Data'] < df['STD3'])), 
             ((df['Data'] > df['STD3'])), 
             ((df['Data'] < df['STD-1']) & (df['Data'] > df['STD-15'])),
             ((df['Data'] < df['STD-15']) & (df['Data'] > df['STD-2'])), 
             ((df['Data'] < df['STD-2']) & (df['Data'] > df['STD-25'])),
             ((df['Data'] < df['STD-25']) & (df['Data'] > df['STD-3'])), 
             ((df['Data'] < df['STD-3']))
             ]

result = [0, 1, 1.5, 2, 2.5, 3, -1, -1.5, 2, -2.5, -3]

df['RESULT'] = np.select(condition, result, None)


When i Run the code it gets the logic wrong and i do not know why? Marked in yellow what returns wrong answer...

[Image: MIcSh.jpg]

Can anyone help to find out what I am doing wrong?
Reply
#2
explain:
  • what you are trying to do ... your goal.
  • spreadsheet name and location
  • Read about the following two packages which allow reading data directly from spreadsheet:
    pandas
    openpyxl
Your attempted code is not the way to go about this.
Reply
#3
(Jul-28-2022, 09:22 AM)Larz60+ Wrote: explain:
  • what you are trying to do ... your goal.
  • spreadsheet name and location
  • Read about the following two packages which allow reading data directly from spreadsheet:
    pandas
    openpyxl
Your attempted code is not the way to go about this.

In the pricture row 0 you see that I try to use the logic presented above to set a number in the result column.
I am checking if the data point is smaller or larger than the other values and set a number in the result column depending on where on how large or small the number is.
for example in row 0, data value -0,028.. is smaller than STD1 and the higher numbers. smaller than STD-1 value but not smaller than STD-15 value.
So it should be returning value: Data" > "STD1" and "Data" < "STD15" = 1
But it returns 0... :S
Reply
#4
To be honest your "excel" code (whatever that means - formulas or VBA) also doesn't make much sense...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
I also think you are being a bit unclear. English is not your language, I suppose. That makes things harder.

Post a csv file with your data, or a few lines of your data. What you want to do seems easy.

You want to compare other columns to the column data.
Reply
#6
(Jul-29-2022, 08:23 AM)Pedroski55 Wrote: I also think you are being a bit unclear. English is not your language, I suppose. That makes things harder.

Post a csv file with your data, or a few lines of your data. What you want to do seems easy.

You want to compare other columns to the column data.

Hey
I am not native speaker of English so sorry for my bad English.

[Image: MIcSh.jpg]

If you look at the dataframe in the picture. For each day i have values.

For example for:
Date 13.05.2022 and the row.
I compair -5 to check if it is bigger than any of the numbers or smaller. Since it is in the middle it should return 0. my code return wrong 2.5..

If data value in this row was -7,5 it would be between STD-2 and STD-15 ( not smaller than STD-2 and smaller than STD-15) this would have me want to return -2,5 as result.
If the value was -11 it would be smaller than STD-3 and i want returned -3.
If the value was -1,5 than it would be bigger than STD2 and smaller than STD25 and I want returned 2.

I want to check if the value under data column is bigger or smaller than other values (bigger or smaller than std columns) and result a value depending on this.

You can think of it as a line of numbers. Most of the time the data value is expected to be in the middle close, but if its bigger
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Logic Error rmfooty 3 945 Dec-05-2022, 01:44 PM
Last Post: rmfooty
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 1,898 Sep-18-2019, 09:43 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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