Use np.where, pandas does intrinsic data alignment, meaning you don't need to use apply or iterate row by row, pandas will align the data on index:
df['new column'] = df['new column'] = np.where(((df['y'] <= .5) | (df['x'] > .5)) & ((df['x'] < .5) | (df['y'] >= .5)), 'Good', 'Bad')
df
Using @YunaA. setup....
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 0.1, 0.1],
'y': [1, 2, 0.7, 0.2],
'column 3': [1, 2, 3, 4]})
df['new column'] = df['new column'] = np.where(((df['y'] <= .5) | (df['x'] > .5)) & ((df['x'] < .5) | (df['y'] >= .5)), 'Good', 'Bad')
df
Output:
x y column 3 new column
0 1.0 1.0 1 Good
1 2.0 2.0 2 Good
2 0.1 0.7 3 Bad
3 0.1 0.2 4 Good
Timings:
import pandas as pd
import numpy as np
np.random.seed(123)
df = pd.DataFrame({'x':np.random.random(100)*2,
'y': np.random.random(100)*1})
def update_column(row):
if (row['x'] >= .5 or row['y'] <= .5) and (row['x'] < .5 or row['y'] >= .5):
return "Good"
return "Bad"
Results
%timeit df['new column'] = np.where(((df['y'] <= .5) | (df['x'] > .5))
& ((df['x'] < .5) | (df['y'] >= .5)), 'Good', 'Bad')
1.45 ms ± 72.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit df['new_column'] = df.apply(update_column, axis=1)
5.83 ms ± 484 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)