Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

adding a pandas column with multiple conditions

# create a list of  conditions
conditions = [
    (df['likes_count'] <= 2),
    (df['likes_count'] > 2) & (df['likes_count'] <= 9),
    (df['likes_count'] > 9) & (df['likes_count'] <= 15),
    (df['likes_count'] > 15)
    ]

# create a list of the values we want to assign for each condition
choices = ['tier_4', 'tier_3', 'tier_2', 'tier_1']

# create a new column and use np.select to assign values to it using our lists as arguments
df['tier'] = np.select(conditions, choices)

# display updated DataFrame
df.head()
Comment

pandas set condition multi columns

conditions = [
   .....:     (df['col2'] == 'Z') & (df['col1'] == 'A'),
   .....:     (df['col2'] == 'Z') & (df['col1'] == 'B'),
   .....:     (df['col1'] == 'B')
   .....: ]
   .....: 

In [214]: choices = ['yellow', 'blue', 'purple']

In [215]: df['color'] = np.select(conditions, choices, default='black')

In [216]: df
Out[216]: 
  col1 col2   color
0    A    Z  yellow
1    B    Z    blue
2    B    X  purple
3    C    Y   black
Comment

python multiple conditions in dataframe column values

df.loc[(df['Salary_in_1000']>=100) & (df['Age']< 60) & (df['FT_Team'].str.startswith('S')),['Name','FT_Team']]
Comment

pandas set condition multi columns

conditions = [
            (df['col2'] == 'Z') & (df['col1'] == 'A'),
            (df['col2'] == 'Z') & (df['col1'] == 'B'),
            (df['col1'] == 'B')
			]


choices = ['yellow', 'blue', 'purple']

df['color'] = np.select(conditions, choices, default='black')

df
# Out[216]: 
#  col1 col2   color
# 0    A    Z  yellow
# 1    B    Z    blue
# 2    B    X  purple
# 3    C    Y   black
Comment

pandas operations with multiple columns

df.query('A in @mylist')
Comment

PREVIOUS NEXT
Code Example
Python :: python input lowercase 
Python :: pyautogui color 
Python :: panda categorical data into numerica 
Python :: python remove nan rows 
Python :: python3 change file permissions 
Python :: save and load a machine learning model using Pickle 
Python :: port 5432 failed: timeout expired 
Python :: sum of number digits python 
Python :: python tkinter define window size 
Python :: python use variable in another file 
Python :: python ftp login 
Python :: how to use print in python 
Python :: calculating mean for pandas column 
Python :: python iterate through dictionary 
Python :: collections counter 
Python :: measure execution time in jupyter notebook 
Python :: how do i print a list line by line in python 
Python :: isnumeric python 
Python :: python numpy array to list 
Python :: capitalise words in a column pandas 
Python :: python calculate derivative of function 
Python :: how to make a numpy array 
Python :: python frame in a frame 
Python :: set form field disabled django 
Python :: if dict.values <= int 
Python :: python manage.py collectstatic --noinput 
Python :: change date format python code 
Python :: time a line of code python 
Python :: difference of two set in python 
Python :: loop through list of tuples python 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =