Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

case statement in pandas

# If the row value in column 'is_blue' is 1 
# Change the row value to 'Yes' 
# otherwise change it to 'No'
df['is_blue'] = df['is_blue'].apply(lambda x: 'Yes' if (x == 1) else 'No') 
# or you can use np.where
df['is_blue'] = np.where(df['is_blue'] == 1, 'Yes', 'No')
# You can also use mapping to accomplish the same result
# Warning: Mapping only works once on the same column creates NaN's otherwise
df['is_blue'] = df['is_blue'].map({0: 'No', 1: 'Yes'})  
Comment

switch cases pandas

df['new_column'] = np.where(df['col2']<9, 'value1',
                   np.where(df['col2']<12, 'value2',
                   np.where(df['col2']<15, 'value3', 'value4')))
Comment

pandas columns case when equivalent

df['c'] = np.select(
[
    (df['a'].isnull() & (df['b'] == 0))
], 
[
    1
], 
default=0 )
Comment

PREVIOUS NEXT
Code Example
Python :: python absolute value 
Python :: selenium get parent element python 
Python :: how to pick out separate columns from the pandas dataframe object 
Python :: python put quotes in string 
Python :: decision tree classifier 
Python :: python numba 
Python :: Read XML file to Pandas DataFrame 
Python :: convert string to utf8 python 
Python :: find record where dataframe column value contains 
Python :: find closest color python 
Python :: python big comment 
Python :: how to get decimal part of a double in python 
Python :: select specific rows from dataframe in python 
Python :: Insert missing data in pandas 
Python :: how to check python version on terminal 
Python :: flatten numpy array 
Python :: networkx largest component 
Python :: install python packages behind proxy 
Python :: Configuring Django to Send Emails with mailgun 
Python :: pygame caption 
Python :: python os.name mac 
Python :: dict itterator python recursive 
Python :: password generator in python 
Python :: python string to list with separator 
Python :: pyspark join 
Python :: python with file 
Python :: how to select python 3 interpreter in linux 
Python :: venv python 
Python :: pyqt5 image 
Python :: python convert timestamp to datetime 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =