New to python. I am trying to figure out the best way to create a column based on other columns. Ideally, the code would be as such.
df['new'] = np.where(df['Country'] == 'CA', df['x'], df['y'])
I do not think this works because it thinks that I am calling the entire column. I tried to do the same thing with apply but was having trouble with syntax.
df['my_col'] = df.apply(
lambda row:
if row.country == 'CA':
row.my_col == row.x
else:
row.my_col == row.y
I feel like there must be an easier way.
np.where()version doesn't work?&and|.lambda row: row.x if row.country == 'CA' else row.y, but thewherething should work. Remember that a lambda should have no side effects -- it is just an expression that returns a value.dfisn't a data frame or those columns have nested objects. Please provide a reproducible example.