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 a value to an existing field in pandas dataframe after checking conditions
# Create a new column called based on the value of another column
# np.where assigns True if gapminder.lifeExp>=50
gapminder['lifeExp_ind'] = np.where(gapminder.lifeExp >= 50, True, False)
gapminder.head(n=3)
add a value to an existing field in pandas dataframe after checking conditions
gapminder['gdpPercap_ind'] = gapminder.gdpPercap.apply(lambda x: 1 if x >= 1000 else 0)
gapminder.head()
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