Horje
pandas create a new column based on condition of two columns Code Example
pandas create new column conditional on other columns
# For creating new column with multiple conditions
conditions = [
    (df['Base Column 1'] == 'A') & (df['Base Column 2'] == 'B'),
    (df['Base Column 3'] == 'C')]
choices = ['Conditional Value 1', 'Conditional Value 2']
df['New Column'] = np.select(conditions, choices, default='Conditional Value 1')
pandas create a new column based on condition of two columns
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




Python

Related
importerror: cannot import name 'adam' from 'keras.optimizers' (/usr/local/lib/python3.7/dist-packages/keras/optimizers.py) site:stackoverflow.com Code Example importerror: cannot import name 'adam' from 'keras.optimizers' (/usr/local/lib/python3.7/dist-packages/keras/optimizers.py) site:stackoverflow.com Code Example
failed to allocate bitmap Code Example failed to allocate bitmap Code Example
A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead Code Example A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead Code Example
discord.py embeds Code Example discord.py embeds Code Example
truncate string python Code Example truncate string python Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7