boolean

Difference between if not: and if False:

Difference between if not: and if False: Question: I have a probably quite simple question but was wondering between the difference of these two statements: if not os.path.isfile(file): #Do some stuff if os.path.isfile(file) is False: #Do some stuff What are the differences (if any) between the two? To my understanding they both return a True …

Total answers: 4

Filtering pandas dataframe with multiple Boolean columns

Filtering pandas dataframe with multiple Boolean columns Question: I am trying to filter a df using several Boolean variables that are a part of the df, but have been unable to do so. Sample data: A | B | C | D John Doe | 45 | True | False Jane Smith | 32 | …

Total answers: 5

Comparing previous row values in Pandas DataFrame

Comparing previous row values in Pandas DataFrame Question: import pandas as pd data={‘col1’:[1,3,3,1,2,3,2,2]} df=pd.DataFrame(data,columns=[‘col1’]) print df col1 0 1 1 3 2 3 3 1 4 2 5 3 6 2 7 2 I have the following Pandas DataFrame and I want to create another column that compares the previous row of col1 to see …

Total answers: 4

Pandas read_csv, reading a boolean with missing values specified as an int

Pandas read_csv, reading a boolean with missing values specified as an int Question: I am trying to import a csv into a pandas dataframe. I have boolean variables denoted with 1’s and 0’s, where missing values are identified with a -9. When I try to specify the dtype as boolean, I get a host of …

Total answers: 3

Syntax for an If statement using a boolean

Syntax for an If statement using a boolean Question: I just recently joined the python3 HypeTrain. However I just wondered how you can use an if statement onto a boolean. Example: RandomBool = True # and now how can I check this in an if statement? Like the following: if RandomBool == True: #DoYourThing And …

Total answers: 3

Is there any legitimate use of list[True], list[False] in Python?

Is there any legitimate use of list[True], list[False] in Python? Question: Since True and False are instances of int, the following is valid in Python: >>> l = [0, 1, 2] >>> l[False] 0 >>> l[True] 1 I understand why this happens. However, I find this behaviour a bit unexpected and can lead to hard-to-debug …

Total answers: 3

Comparing boolean and int using isinstance

Comparing boolean and int using isinstance Question: Can someone give me an explanation why isinstance() returns True in the following case? I expected False, when writing the code. print isinstance(True, (float, int)) True My guess would be that its Python’s internal subclassing, as zero and one – whether float or int – both evaluate when …

Total answers: 6

Converting "true" (JSON) to Python equivalent "True"

Converting "true" (JSON) to Python equivalent "True" Question: The Train status API I use recently added two additional key value pairs (has_arrived, has_departed) in the JSON object, which caused my script to crash. Here’s the dictionary: { “response_code”: 200, “train_number”: “12229”, “position”: “at Source”, “route”: [ { “no”: 1, “has_arrived”: false, “has_departed”: false, “scharr”: “Source”, …

Total answers: 8