make a condition statement on column pandas
df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]
make a condition statement on column pandas
df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]
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')
Add new column based on condition on some other column in pandas.
# np.where(condition, value if condition is true, value if condition is false)
df['hasimage'] = np.where(df['photos']!= '[]', True, False)
df.head()
make a condition statement on column pandas
df.loc[df['column name'] condition, 'new column name'] = 'value if condition is met'
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us