0

I have been trying to optimise this, but since each if has its own condition, i am not able to reduce its complexity

 for () in ():
 
 if ():
 if ():
 #Logical condition
 if ():
 #Logical condition
 else:
 #Logical condition
 
#another condition
 if (): 
 if (): 
 if ():
 #Logical condition
 if index in ():
 #Logical condition
Edo Akse
4,4052 gold badges13 silver badges25 bronze badges
asked Jul 15, 2021 at 12:50
2
  • Please, take a while to read How to make good reproducible pandas examples Commented Jul 15, 2021 at 12:53
  • Multiple if in a row should be combined with &. if cond1: if cond2: if cond3 is the same as if cond1 & cond2 & cond3: Commented Jul 15, 2021 at 12:56

1 Answer 1

1

Logic operators are your friend. and, or, xor, etc

for () in ():
 if () and ():
 #Logical condition
 if ():
 #Logical condition
 pass
 else:
 #Logical condition
 pass
#another condition
 if () and () and () and index in ():
 #Logical condition
 pass
answered Jul 15, 2021 at 12:57
Sign up to request clarification or add additional context in comments.

3 Comments

Hello! Thank you for Answering. While adding the next condition, there are two logical conditions I need to add... just let me know how to add the second one as well. Thanks again!
which next condition are you talking about? It's not exactly clear from how you describe it. That being said, if you have 2 conditions that both need to be met, you would use if condition1 == True and condition2 == True
I tried your method but the execution time for 1000 data has increased from 65 seconds to 72 seconds which is undesired. Do let me know the alternatives

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.