df[df['A'] > 2]['B'] = new_val # new_val not set in df # rewrite it as below df.loc[df['A'] > 2, 'B'] = new_val