Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a dataframe which looks like this,
df=pd.DataFrame([2,4,6,2],columns=['x'])
df['y']=[3,2,1,2]
df['x_test']=['x_larger','x_larger','x_smaller','x_equal']
I am trying to do a if/then/else, similar to this idiom and cookbook. I am able to get one if statement correct but I am not sure how to test for equal.
df['valid']=False
df['invalid']=True
df.ix[(df.x_test=="x_larger") & (df.x>df.y),['valid','invalid']]=[True,False]
This works partially. But I would also like to test for x_equal in this line. Is that possible?
The desired output should be
x y x_test valid invalid
0 2 3 x_larger False True
1 4 2 x_larger True False
2 6 1 x_smaller False True
3 2 2 x_equal True False
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
When I try to run print(round(x, 2)) it shows as 0.0.
It will round to 0 if x is smaller then 0.005.
x = 0.005
round(x, 2)
> 0.01
x = 0.0049
round(x, 2)
> 0.00
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to print the numbers 1 to 10 with a for loop, but import random is affecting my for loop and the loop is running twice. Here is a screenshot of my editor. The output is displayed below. Can you explain to me why this happens?
import random
for i in range(1,10):
print(i)
My output:
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
This will never happen.
make sure you are running the latest version of your program, and make sure you didn't paste the same code at some other place in the same file. e.g. make sure you don't have a precompiled version of it. (e.g. change the file name and try again).
Update:
based on the comment by matthias, if you saved your file as random.py the above result could be reproduced.
# if you save the code below as random.py
import random
for i in range(1,10):
print(i)
# your output could be reproduced as below.
>>> python random.py
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
solution: change the file name or if you have to import it make sure you include the codition if __name__ == "__main__" in before the for loop of your random.py
11I do not see any problem, I did the example and it works perfect.
My output:
1
2
3
4
5
6
7
8
9
Could it be that you have called the file twice? It's the only request I find.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am new to programming and have just briefly learned functions in python. I do not understand why print(len(str)) don't work the same as return len(str) in a function.
I have tried both print and return for the last statement of the function and am confused about my understanding of len(). I need some guidance, thank you! Perhaps someone can guide me as to how I can further improve my foundation as I am still pretty new to programming. Thank you!!
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
numDigits(833)
If I change the
print to return len(str_digits) and
numDigits(833) to print(numDigits(833)),
I get my expected answer.
What i expected:
3
Actual result:
3
None
1
2
4
3
In the first case numDigits doesn't return a value from the function, and you only print it inside the function
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
print(numDigits(833))
The output here is
3
None
The 3 comes from print and None comes from the function, and when you print it, it prints None
If you want to return, you need a return statement like return len(str_digits) at the end of the function like so
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
#Return statement
return len(str_digits)
print(numDigits(833))
The output will now be
3
3
Now the first 3 comes from print, and the second 3 comes when you print what numDigits return, which is 3
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
so I'm trying to create as short as possible code for printing an input backwards, and I want to go below 60B. My code takes 79B, and have no idea if it is actually possible to shorten it even more.
tab=[i for i in map(int,input().split())]
print(" ".join(map(str, tab[::-1])))
So when I input:
1 2 3 4 5
I get in output:
5 4 3 2 1
Anybody got idea if it can be even shorter?
print(*input().split()[::-1])
Splits the list by spaces, then reverses and sends to print as a bunch of arguments.
print supplies the separating space automatically.
One liner 41 Bytes
>>> print(''.join(i for i in input()[::-1]))
1 2 3 4 5
5 4 3 2 1
Altough, you could just reverse the input() output w/ subscripting, and print that.
>>> print(input()[::-1])
1 2 3 4 5
5 4 3 2 1
Maybe I don't understand what you're asking, but if it's just to reverse the string as such, then print(input()[::-1]) clocks in at 21B.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
so I was just looking over a few problems where I had to interpret a piece of code, and I stumbled upon this one :
a = 10
b = 4
c = 2
d = 3
if ((c+2<d)) or ((c*d)==(a-b)):
if (True and not(True)) or True:
print ("X")
else:
print("Y")
print("Z")
I understand that the first part of the statement will evaluate to False, while the second part will evaluate to True. The problem I am having is about interpreting the if statement that follows. What is the "True and False" or "True" referring to, the previous statement? Thanks
First, you need a closing parenthesis after that last True.
Second, that second if statement will always evaluate to True, because of the or True part. It's not referring to anything. It's just using the Python built-in constant True.
Here's a corrected version of that code. It runs, but it doesn't accomplish anything significant. It will always print "X" and "Z".
a = 10
b = 4
c = 2
d = 3
if ((c+2<d)) or ((c*d)==(a-b)):
if (True and not(True) or True):
print ("X")
else:
print("Y")
print("Z")