Wednesday 15 February 2012

python - Pandas: how to match multiple pattern (OR) with np.where -



python - Pandas: how to match multiple pattern (OR) with np.where -

i know if possible np.where in pandas match multiple patterns kind of 'or' argument

for exemple seek create new column in dataframe called 'kind' , each rows fill "test" if value in column called 'label' match of listed patterns otherwise fill "control".

i'm using this:

df['kind'] = np.where(df['label'] == 'b85_c', 'test', 'control')

and working 1 pattern

what i'm looking after this:

df['kind'] = np.where(df['label'] == 'b85_c'or'b85_n' ,'test', 'control')

any ideas how perform or if there alternatives? thanks

you can either utilize bitwise or:

(df['label'] == 'b85_c') | (df['label'] == 'b85_n')

or can utilize isin method:

df['label'].isin(['b85_c', 'b85_n'])

python pandas

No comments:

Post a Comment