This question already has answers here:
pandas logical and operator with and without brackets produces different results [duplicate]
(2 answers)
Logical operators for Boolean indexing in Pandas
(4 answers)
Closed 3 years ago.
I was trying to find records based on two conditions on a data frame preg
First:
preg[preg.caseid==2298 & preg.pregordr==1]
This throws and error that truth value of a series is ambiguous.
Why?
Second:
But this one works!
preg[(preg.caseid==2298) & (preg.pregordr==1)]
So what exactly is the difference between the two?
Because it thinks that you're doing 2298 & preg.pregordr something like that, without parenthesis you can do:
preg[preg.caseid.eq(2298) & preg.pregordr.eq(1)]
Related
This question already has answers here:
The right way to round pandas.DataFrame?
(3 answers)
round a single column in pandas
(8 answers)
Closed 2 years ago.
I would like to check how may we use the simple basic round() function to round values in a pandas dataframe to a specific number of decimal points. I kept playing around but I couldn't get it right and I am very new to Python so am not looking for anything too sophisticated.
I understand if you use round(3.986, 2) it will simply output to 2 d.p. as 3.99.
And I know we may access the df values through df.values.
I tried exploring the df.applymap() function too.
Help !
You can use apply on the column which you want to round the numbers
>>> df['Numeric Column'].apply(lambda x : math.round(x,2))
This will give you the intended result
This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
Using "and" and "or" operator with Python strings
(6 answers)
Closed 2 years ago.
I am modifying some Python code that is used in an ODOO Addon.
How does the logic for the following statement work?
origin = (move.group_id and move.group_id.name or (move.origin or move.picking_id.name or "/"))
Values
move.group_id is an object
move.group_id.name is a string value
move.origin is a string value
move.picking_id.name is a string
the result is "origin" is assigned the string value of "move.group_id.name"
What is this kind of assignment called?
(I tried to google this first but its not inline if logic and I have a lack of words for what this is called.)
This question already has answers here:
Tilde sign in pandas DataFrame
(4 answers)
Logical operators for Boolean indexing in Pandas
(4 answers)
How can I obtain the element-wise logical NOT of a pandas Series?
(6 answers)
Closed 1 year ago.
Watching this piece of code in the book:
def split_train_test_by_id(data, test_ratio, id_column, hash=hashlib.md5):
ids = data[id_column]
in_test_set = ids.apply(lambda id_: test_set_check(id_, test_ratio, hash))
return data.loc[~in_test_set], data.loc[in_test_set]
Never saw this loc[~<..>] before. Probably understanding the functionality, however want to be sure. Also is it working only in pandas or python in general?
I saw some great comments above, but wanted to make sure that it's clear for a beginner. The ~ flips 1s to 0s and 0s to 1s. It is commonly used with pandas to signify not. In your example, ~in_test_set is similar to saying not in_test_set. The advantage to ~ is that it works with a set of values and is not limited to a single value. See the Python wiki on bitwise operators.
This question already has answers here:
How to filter Pandas dataframe using 'in' and 'not in' like in SQL
(11 answers)
Selecting with complex criteria from pandas.DataFrame
(5 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
I have been trying to change the words in one of the columns of my data frame such that it will replace all the words except some keywords which I want to leave as it is.
I tried doing this:
df['col]=df['col].where(lambda x: x == ('A'or'B'or'C'),'others')
My plan is to replace every word that isn't A or B or C with others.
The code worked for A but B and C were replaced.
How do I go about this please?
NB: I am trying to improve my python skill and have searched the entire stack-overflow before asking this question.
Thank you for your help.
This question already has answers here:
Spark Equivalent of IF Then ELSE
(4 answers)
Closed 5 years ago.
I am trying to use a "chained when" function.
In other words, I'd like to get more than two outputs.
I tried using the same logic of the concatenate IF function in Excel:
df.withColumn("device_id", when(col("device")=="desktop",1)).otherwise(when(col("device")=="mobile",2)).otherwise(null))
But that doesn't work since I can't put a tuple into the "otherwise" function.
Have you tried:
from pyspark.sql import functions as F
df.withColumn('device_id', F.when(col('device')=='desktop', 1).when(col('device')=='mobile', 2).otherwise(None))
Note that when chaining when functions you do not need to wrap the successive calls in an otherwise function.