apply lambda if code example
Example 1: apply lambda with if
df['new column name'] = df['column name'].apply(lambda x: 'value if condition is met' if x condition else 'value if condition is not met')
Example 2: make a condition statement on column pandas
df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]
Example 3: if condition dataframe python
df.loc[df['age1'] - df['age2'] > 0, 'diff'] = df['age1'] - df['age2']
Example 4: if condition in python lambda
func = lambda element: (expression and DoSomething) or DoSomethingIfExpressionIsFalse