np.select with multiple conditions

conditions = [
    df['gender'].eq('male') & df['pet1'].eq(df['pet2']),
    df['gender'].eq('female') & df['pet1'].isin(['cat', 'dog'])
]

choices = [5,5]

df['points'] = np.select(conditions, choices, default=0)

print(df)
     gender      pet1      pet2  points
0      male       dog       dog       5
1      male       cat       cat       5
2      male       dog       cat       0
3    female       cat  squirrel       5
4    female       dog       dog       5
5    female  squirrel       cat       0
6  squirrel       dog       cat       0

3.67
9
Awgiedawgie 440220 points

                                    #Using .assign you can make multiple
#operations that depend on the
#previous ones without the need
#of creating intermediate variables
import pandas as pd

df = pd.DataFrame({
    'name': ['alice','bob','charlie','daniel'],
    'age': [25,66,56,78]
})

df.assign(
    is_senior = lambda dataframe: dataframe['age'].map(lambda age: True if age >= 65 else False) 
).assign(
    name_uppercase = lambda dataframe: dataframe['name'].map(lambda name: name.upper()),
).assign(
    name_uppercase_double = lambda dataframe: dataframe['name_uppercase'].map(lambda name: name.upper()+"-"+name.upper())
)

3.67 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
set value of column dataframe based on two other columns pandas add column based on condition of other columns add two column conditions pandas pandas assign value to multiple column based on condition pandas apply condition of two columns. and two columns pandas create dataframe with 2 columns create new column based on condition pandas create two new columns panda dataframe how to create a new column by adding two other columns pandas pandas add column based on two other columns create new column pandas based on condition add new column in dataframe based on condition from another dataframe add new column in dataframe based on condition from another add column from one dataframe to another based on condition pandas combining two column with if statements create column based on condition on two other columns pandas create dataframe with single column how to create a new column based on multiple condition pandas create new column based on condition pandas fastest way pandas two columns condition python new column if two columns are true create a new column based on condition in pandas create 2 column dataframe pandas apply condition on two columns pandas adding a new column in pandas based on conditions of multiple columns adding a new column in pandas based on condition of multiple columns compare two dataframe and add new columns pandas create new column based on two other columns condition where 2 columns pandas pandas create two new columns from apply pandas np select multiple conditions how to add a new column in pandas dataframe by applying some condition on different column using multiple conditions in np.where pandas add column to dataframe based on condition numpy select multiple conditions np.select multiple conditions np select multiple conditions pandas Pandas set column to another column based on condition concatinate 2 column to one pandas with condition apply if condition on 2 columns in pandas pandas new column conditional on other columns pandas combine two dataframes based on column dataframe create new column based on condition for some rows pandas create a column from 2 different columns based on condition np.where multiple conditions multiple results pandas add column based on calculation from two other columns combine two dataframes pandas based on column how to combine pandas columns for condition python dataframe new column based on other columns with different row create a new integer column based on multiple condition pandas create a new new column based on multiple condition pandas create a new dataframe with 2 columns np.where with multiple conditions how do i make one column equal another column in two pandas dataframe pandas create column based on multiple conditions add new column to dataframe based on condition pandas assign one column or the other depending on condition pandas if pandas new column from two columns when at least one of them has value pandas create new column with multiple if statement pandas combine 2 column values as new colum python add new column condition on 2 columns rows mutate column in pandas pandas dataframe create a new column from calculation of two other columns for each row combine values of two columns in pandas pandas dataframe create column based on condition pandas create column based on conditions of other columns create a column based on two column pandas python pandas new column based on two condition columns condition how to merge two dataframes in pandas based on one column value create new column from calculation on 2 other columns in pandas pandas apply two columns np select multiple conditions pandas create a new column by adding 2 other columns merge two columns in pandas dataframe based on condition python pandas create column based on condition pandas create a new column based on comparison of two other columns pd conditional two columns make new column based on condition pandas dataframe compare two columns to add a new column np.select with multiple conditions pandas create new column based on other columns pandas dataframe compare two columns and insert value to new column pandas create column based on condition from another column how do I add a new colnum in dataframe based on the condition of some other colnum pandas create a column based on another condition create new column from two columns pandas pandas merge two columns on condition pandas create new column based on condition how to make a dataframe pandas from two existing columns pandas create new columns condition pandas new column based on another column condition pandas create new column based on multiple conditions create new dataframe based on multiple column value pandas new column if two columns are equal if statement across multiple columns pandas create new column based on multiple condition if else pandas create new column conditional on other columns create dataframe with existing columns of two dataframes inE create dataframe with existing columns of two dataframes mutate one column of pandas dataframe create new column in pandas based on two columns create new column in pandas dataframe based on multiple condition create new column in pandas based on condition create new column based on multiple condition pandas pandas new column based on two columns if two columns are equal then add two other columns pandas pandas create column on condition in df if three columns data apply my condition create new column create a column in python based on condition on other column how to create a column of a dataframe based on a condition create several columns based on condition pandas create new column in pandas dataframe based on condition how to create a new column in pandas based on a condition create a new column based on condition in two dataframe pandas create 3 new column based other row condition pandas create 3 new column based on condition combine 1 column dataframe with another 1 cooumn dataframe pandas put columns in variable pandas how to add a data to a dataset in python in if else create new column based on two columns pandas create a column based on condition and another dataframe pandas add a column to a pandas dataframe based on an if-else condition create pandas field into a if condition how to create a new dataframe with if statement pandas create new column based on multiple condition how to make condition in new column df in pandas create a new column in pandas based on condition apply if statement in a column of dataset create new column in python based on condition create column with if statement python create new variable df python pandas create a column based on condition add value new column to dataframe based on condition pandas merge rows based on condition how to flag 0 or 1 if some condition is met in python dataframe pandas select columns by condition if column contains two values then create new column with value python df create pandas column with new values based on values in other columns select condition 2 column dataframe pandas if condition on column and create new column applying two conditions pandas dataframe python create new column based on condition create a new column in pandas by a condition of another if old column is between interval then create new column in python set new column based on conditional pandas how to make add a column to a dataframe in python conditional assign a value in dataframe from inside a function pandas new column based on multiple conditions pandas create column based on condition create column given condition pandas select data based on condition in two columns pandas append columns dataframe based on condition Select DataFrame Rows Based on multiple conditions on columns python add a column in pandas based on condition pandas calculate new column based on old column if else pandas mutate column create a column based on a conditional in pandas pandas mutate new column
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source