conditional

Pandas/Python: Set value of one column based on value in another column

Set value of one Pandas column based on value in another column Question: I need to set the value of one column based on the value of another in a Pandas dataframe. This is the logic: if df[‘c1’] == ‘Value’: df[‘c2’] = 10 else: df[‘c2’] = df[‘c3’] I am unable to get this to do …

Total answers: 10

Programming style & avoiding null values

Programming style & avoiding null values Question: So I’m working my way through Wentworth et al How to Think Like a Computer Scientist a Python 3 guidebook to try and teach myself more about programming. While it’s a fantastic resource, it has very little to say about style and "best practice" for writing in Python …

Total answers: 5

Using or in if statement (Python)

Using or in if statement (Python) Question: I’m just writing a simple if statement. The second line only evaluates to true if the user types “Good!” If “Great!” is typed it’ll execute the else statement. Can I not use or like this? Do I need logical or? weather = input(“How’s the weather? “) if weather …

Total answers: 1

How would I confirm that a python package is installed using ansible

How would I confirm that a python package is installed using ansible Question: I am writing an ansible playbook, and I would first like to install pew, and transition into that pew environment in order to install some other python libraries. So, my playbook is going to look something like this… tasks: # task 1 …

Total answers: 2

Data type conditions in python

Data type conditions in python Question: How do i give a condition in which for example; if x is not an integer print(“type an integer”) Asked By: ebere || Source Answers: With your sample code, your best bet is to catch the ValueError and try again: def get_int(): try: return int(input(‘Type an integer:’)) except ValueError: …

Total answers: 2

Why is this simple conditional expression not working?

Why is this simple conditional expression not working? Question: Very simple line: i = 3 a = 2 if i in [1, 3, 6] else a = 7 fails with: SyntaxError: can’t assign to conditional expression whereas expanded as: if i in [1, 3, 6]: a = 2 else: a = 7 works fine. Asked …

Total answers: 2

Conditional with statement in Python

Conditional with statement in Python Question: Is there a way to begin a block of code with a with statement, but conditionally? Something like: if needs_with(): with get_stuff() as gs: # do nearly the same large block of stuff, # involving gs or not, depending on needs_with() To clarify, one scenario would have a block …

Total answers: 9

Python prime number calculator

Python prime number calculator Question: prime = [2] while len(prime) <= 1000: i=3 a = 0 for number in prime: testlist= [] testlist.append(i%number) if 0 in testlist: i=i+1 else: prime.append(i) i=i+1 print(prime[999]) Trying to make a program that computes primes for online course. This program never ends, but I can’t see an infinite loop in …

Total answers: 3

when to use if vs elif in python

when to use if vs elif in python Question: If I have a function with multiple conditional statements where every branch gets executed returns from the function. Should I use multiple if statements, or if/elif/else? For example, say I have a function: def example(x): if x > 0: return ‘positive’ if x < 0: return …

Total answers: 7

Creating a new column based on if-elif-else condition

Creating a new column based on if-elif-else condition Question: I have a DataFrame df: A B a 2 2 b 3 1 c 1 3 I want to create a new column based on the following criteria: if row A == B: 0 if rowA > B: 1 if row A < B: -1 so …

Total answers: 6