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)
Answer from Gustavo Bezerra on Stack Overflow
🌐
Medium
medium.com › @michalwesleymnach › the-complete-guide-to-create-columns-based-on-multiple-conditions-in-pandas-dataframes-eedf2c0392a6
The complete guide to creating columns based on multiple conditions in a Pandas DataFrame | by Michaël Ménaché | Medium
July 17, 2022 - Note that the conditional operator can also be used in a function with .apply(). It’s just another way of writing the function. ... .loc[] is usually one of the first things taught about Pandas and is traditionally used to select rows and columns. But it can also be used to create new columns:
🌐
Dataquest
dataquest.io › home › blog › tutorial: add a column to a pandas dataframe based on an if-else condition
Add a Column in a Pandas DataFrame Based on an If-Else Condition
March 6, 2023 - This means that the order matters: if the first condition in our conditions list is met, the first value in our values list will be assigned to our new column for that row. If the second condition is met, the second value will be assigned, et cetera.
Discussions

Add new column to Python Pandas DataFrame based on multiple conditions - Stack Overflow
I have a dataset with various columns as below: discount tax total subtotal productid 3.98 1.06 21.06 20 3232 3.98 1.06 21.06 20 3232 3.98 6 ... More on stackoverflow.com
🌐 stackoverflow.com
pandas - Create new columns based on multiple conditions in Python - Stack Overflow
I have the following dataframe: data = [ (27450, 27450, 29420,"10/10/2016"), (29420 , 36142, 29420, "10/10/2016"), (11 , 11, 27450, "10/10/2016")] #Create DataFrame base df = pd.DataFrame(data, More on stackoverflow.com
🌐 stackoverflow.com
Adding a new column based on conditions (PANDAS)
Do you already have a dictionary or other data structure that connects each country to its continent? More on reddit.com
🌐 r/learnpython
6
5
April 24, 2023
dataset - Creating new column in dataframe based on conditions in 2 other columns - Data Science Stack Exchange
I would like to create a new column in my dataframe based on values from both the gender and experimental_grouping columns. As I have it written below, the column df['group_gender'] has 'control_... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
June 29, 2019
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › python-creating-a-pandas-dataframe-column-based-on-a-given-condition
Python | Creating a Pandas dataframe column based on a given condition - GeeksforGeeks
October 30, 2025 - condition = df['Event'].isin(['Music', 'Comedy']) df = df.assign(Category=lambda x: 'Entertainment' if condition.any() else 'Literature') print(df)
🌐
Statology
statology.org › home › pandas: create new column using multiple if else conditions
Pandas: Create New Column Using Multiple If Else Conditions
October 10, 2022 - You can use the following syntax ... #define conditions conditions = [ (df['column1'] == 'A') & (df['column2'] < 20), (df['column1'] == 'A') & (df['column2'] >= 20), (df['column1'] == 'B') & (df['column2'] < 20), (df['column1'] == 'B') & (df['column2'] >= 20) ] #define results ...
🌐
Kanoki
kanoki.org › 2022 › 06 › 28 › pandas-create-new-column-based-on-value-in-other-columns-with-multiple-conditions
Pandas create new column based on value in other column with multiple conditions | kanoki
June 28, 2022 - Here are the three different ways in which a new column could be created from other columns based on multiple conditions · Create a new column using a custom function with condtions and return value to apply across all the rows of dataframe · Use numpy select with list of conditions and their ...
🌐
Statology
statology.org › home › how to create a new column based on a condition in pandas
How to Create a New Column Based on a Condition in Pandas
May 3, 2022 - The following code shows how to create a new column called ‘Good’ where the value is: ... #define function for classifying players based on points def f(row): if row['points'] < 15: val = 'no' elif row['points'] < 25: val = 'maybe' else: val = 'yes' return val #create new column 'Good' using the function above df['Good'] = df.apply(f, axis=1) #view DataFrame df rating points assists rebounds Good 0 90 25 5 11 yes 1 85 20 7 8 maybe 2 82 14 7 10 no 3 88 16 8 6 maybe 4 94 27 5 6 yes 5 90 20 7 9 maybe 6 76 12 6 6 no 7 75 15 9 10 maybe 8 87 14 9 10 no 9 86 19 5 7 maybe
Find elsewhere
Top answer
1 of 2
2
import numpy as np
import pandas as pd

data = [(27450, 27450, 29420,"10/10/2016"),
        (29420 , 36142, 29420, "10/10/2016"),
        (11 , 11, 27450, "10/10/2016")] 
df = pd.DataFrame(data, columns=("User_id","Actor1","Actor2", "Time"))
mask = (df['User_id'] == df['Actor1'])
df['first actor'] = mask.astype(int)
df['other actor'] = np.where(mask, df['Actor2'], df['Actor1'])
print(df)

yields

   User_id  Actor1  Actor2        Time  first actor  other actor
0    27450   27450   29420  10/10/2016            1        29420
1    29420   36142   29420  10/10/2016            0        36142
2       11      11   27450  10/10/2016            1        27450

First create a boolean mask which is True when User_id equals Actor1:

In [51]: mask = (df['User_id'] == df['Actor1']); mask
Out[51]: 
0     True
1    False
2     True
dtype: bool

Converting mask to ints creates the first column:

In [52]: mask.astype(int)
Out[52]: 
0    1
1    0
2    1
dtype: int64

Then use np.where to select between two values. np.where(mask, A, B) returns an array whose ith value is A[i] if mask[i] is True, and B[i] otherwise. Thus, np.where(mask, df['Actor2'], df['Actor1']) takes the value from Actor2 where mask is True, and the value from Actor1 otherwise:

In [53]: np.where(mask, df['Actor2'], df['Actor1'])
Out[53]: array([29420, 36142, 27450])
2 of 2
0

Heres my solution - I have assumed that if userid appears in actor1 column its not necessary it'll be in the same row...

df["Col1"] = [1 if i in df["Actor1"].values else 0 for i in df["User_id"].values]
df["Col2"] = [df.iloc[i]["Actor2"] if j == 1 else df.iloc[i]["Actor1"] for i, j in enumerate(df["Col1"].values)]

Output -

User_id  Actor1  Actor2        Time  Col1   Col2
0    27450   27450   29420  10/10/2016     1  29420
1    29420   36142   29420  10/10/2016     0  36142
2       11      11   27450  10/10/2016     1  27450
🌐
YouTube
youtube.com › watch
Create new Column by Multiple Conditions | Pandas | DataFrame - YouTube
How to create a new column by multiple conditions using np.where()
Published: June 21, 2022
🌐
Reddit
reddit.com › r/learnpython › adding a new column based on conditions (pandas)
r/learnpython on Reddit: Adding a new column based on conditions (PANDAS)
April 24, 2023 -

I have a column named destination that has over 50 countries. I am trying to add a new column called continents that has each country categorized based on the location. I have used the loc method, but I was only able to add a county one by one.

ex.loc[ex['Destination'] =='MALAYSIA','Continent']='Asia'

Any ideas on this? Thanks

🌐
ProjectPro
projectpro.io › recipes › insert-new-column-based-on-condition-in-python
How to insert a new column based on condition in Python? -
August 23, 2023 - In Python Pandas, new column based on another column can be created using the where() method. The where() method takes a condition and a value as arguments. If the condition is met, then the value is returned.
🌐
Predictivehacks
predictivehacks.com › pandas-how-to-assign-values-based-on-multiple-conditions-of-different-columns
Pandas: How to assign values based on multiple conditions of different columns – Predictive Hacks
May 11, 2020 - Here, we will provide some examples of how we can create a new column based on multiple conditions of existing columns. For these examples, we will work with the titanic dataset. import pandas as pd import numpy as np url = 'https://gist.githubusercontent.com/michhar/2dfd2de0d4f8727f873422c5d959fff5/raw/ff414a1bcfcba32481e4d4e8db578e55872a2ca1/titanic.csv' df = pd.read_csv(url, sep="\t") df.head()
🌐
Arab Psychology
scales.arabpsychology.com › home › how to create new column using multiple if else conditions in pandas
How To Create New Column Using Multiple If Else Conditions In Pandas
November 23, 2025 - Pandas can be used to create new columns using multiple if else conditions by using the ‘np.where()’ function. This function takes three arguments: a boolean condition to check, the value if the condition is True, and the value if the condition is False. The output of this function will be the new column that is created.
🌐
sqlpey
sqlpey.com › python › pandas-new-column-conditional-logic
Pandas Create New Column Based on Multiple Conditions - …
July 25, 2025 - Explore efficient methods for creating a new column in a Pandas DataFrame based on complex conditional logic, comparing `apply`, `loc`, `numpy.select`, and `case_when`.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-create-new-values-in-a-pandas-dataframe-column-based-on-values-from-another-column
How to create new values in a pandas dataframe column based on values from another column | Saturn Cloud Blog
May 1, 2026 - While the apply() function is a powerful tool for creating new columns based on existing ones, an alternative method using numpy and np.select() can provide a more concise and efficient solution, especially when dealing with multiple conditions.
🌐
Towards Data Science
towardsdatascience.com › home › latest › the ultimate guide for column creation with pandas dataframes
The Ultimate Guide for Column Creation with Pandas DataFrames | Towards Data Science
February 5, 2025 - Part 2: Conditions and Functions Here you can see how to create new columns with existing or user-defined functions. If you work with a large dataset and want to create columns based on conditions in an efficient way, check out number 8! Part 3: Multiple Column Creation It is possible to create multiple columns in one line.
🌐
Stack Exchange
datascience.stackexchange.com › questions › 54747 › creating-new-column-in-dataframe-based-on-conditions-in-2-other-columns
dataset - Creating new column in dataframe based on conditions in 2 other columns - Data Science Stack Exchange
June 29, 2019 - -the problem with an inaccurate filling of column group_gender is that in df['group_gender'] = 'dp_m' in the following code, if i == 'M' you are filling the whole column with dp_m, instead you should use methods like iloc but it is not really an efficient way specifically when having a large dataset. In following, I have provided a better way. for i in df['gender']: if i == 'M': df['group_gender'] = 'dp_m' else: df['group_gender'] = 'dp_f' ... #creating an instant dataframe import pandas as pd d = {'gender':['M','M','F', 'M', 'F','M']} df = pd.DataFrame(d) #filling the 'group_gender' column df.loc[df['gender'] == 'M', 'group_gender'] = 'dp_m' df.loc[df['gender'] == 'F', 'group_gender'] = 'dp_f'
🌐
Seo-monster
seo-monster.com › dj5 › pandas-create-new-column-based-on-multiple-condition.html
Pandas create new column based on multiple condition
You can also pass inplace True argument to the function to modify the original DataFrame. df 39 AvgRating 39 df 39 Rating 39 df 39 Metascore 39 10 2 Using the AND Function with Conditional Formatting. 8k points pandas Oct 10 2019 To create new column based on values from other columns in pandas you need two steps to this first is to write a function that does the translation you want I 39 ve put an example together based on your pseudo code Create One Column From Multiple Columns In Pandas Pandas creating new column based on if condition I have the following dataframe and I want to create a column 39 poster 39 that shows the user if comment_id np.
🌐
Stack Overflow
stackoverflow.com › questions › 63681026 › create-new-column-in-pandas-depending-on-multiple-conditions
loops - Create new column in pandas depending on multiple conditions - Stack Overflow
Let's say I have a df where column A can equal any of the following: ['Single', 'Multiple', 'Commercial', 'Domestic', 'Other'], column B has numeric values from 0-30.
🌐
datagy
datagy.io › home › python posts › set pandas conditional column based on values of another column
Set Pandas Conditional Column Based on Values of Another Column • datagy
February 23, 2022 - Lean how to create a Pandas conditional column use Pandas apply, map, loc, and numpy select in order to use values of one or more columns.