I would like to create a new column in a dataframe equal to column A if column C = 'a', and column B if column C = 'b'. I have implemented this:
def f(row):
if row['C'] = 'a':
return row['A']
elif row['C'] = 'b':
return row['B']
return np.nan
df['new'] = df.apply(f, axis=1)
I feel as though the code runs slowly. The answer here explains that this is not vectorized.
Alternatively:
df.ix[df[C]=='a',df['new']] = df.ix[df[C]=='a',df['A']]
df.ix[df[C]=='b',df['new']] = df.ix[df[C]=='b',df['B']]
Is this vectorized? Is there a different 'correct' way of doing this in pandas? What would a vectorized function do differently?