python - Create a column based on condition pertaining to 2 other columns -
i have 2 columns in pandas dataframe (let's call 'col1' , col2'). both contain true/false values.
i need create third column these 2 ('col3'), have true value record if 1 or other of 2 columns has true value in record.
currently, i'm doing with:
col3 = [] index, row in df.iterrows(): if df.ix[index, 'col1'] == true or df.ix[index, 'col2'] == true: col3.append(true) else: col3.append(false) df['col3'] = col3
it works fast enough size of dataset, there way in one-liner/vectorized way? perhaps using 2 nested np.where()
statements?
you can use np.logical_or
this:
in [236]: df = pd.dataframe({'col1':[true,false,false], 'col2':[false,true,false]}) df out[236]: col1 col2 0 true false 1 false true 2 false false in [239]: df['col3'] = np.logical_or(df['col1'], df['col2']) df out[239]: col1 col2 col3 0 true false true 1 false true true 2 false false false
or use |
operator:
in [240]: df['col3'] = df['col1'] | df['col2'] df out[240]: col1 col2 col3 0 true false true 1 false true true 2 false false false
Comments
Post a Comment