Add new column to Python Pandas DataFrame based on multiple conditions
Add new column to Python Pandas DataFrame based on multiple conditions
You can apply an arbitrary function across a dataframe row using DataFrame.apply
.
In your case, you could define a function like:
def conditions(s):
if (s[discount] > 20) or (s[tax] == 0) or (s[total] > 100):
return 1
else:
return 0
And use it to add a new column to your data:
df_full[Class] = df_full.apply(conditions, axis=1)
Judging by the image of your data is rather unclear what you mean by a discount
20%.
However, you can likely do something like this.
df[class] = 0 # add a class column with 0 as default value
# find all rows that fulfills your conditions and set class to 1
df.loc[(df[discount] / df[total] > .2) & # if discount is more than .2 of total
(df[tax] == 0) & # if tax is 0
(df[total] > 100), # if total is > 100
class] = 1 # then set class to 1
Note that &
means and
here, if you want or
instead use |
.