I would like to create a new column with a numerical value based on the following conditions:
a. if gender is male & pet1=pet2, points = 5
b. if gender is female & (pet1 is 'cat' or pet1='dog'), points = 5
c. all other combinations, points = 0
gender pet1 pet2 0 male dog dog 1 male cat cat 2 male dog cat 3 female cat squirrel 4 female dog dog 5 female squirrel cat 6 squirrel dog cat
I would like the end result to be as follows:
gender pet1 pet2 points 0 male dog dog 5 1 male cat cat 5 2 male dog cat 0 3 female cat squirrel 5 4 female dog dog 5 5 female squirrel cat 0 6 squirrel dog cat 0
How do I accomplish this?
You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.
You can extract a column of pandas DataFrame based on another value by using the DataFrame. query() method. The query() is used to query the columns of a DataFrame with a boolean expression.
You can do this using np.where
, the conditions use bitwise &
and |
for and
and or
with parentheses around the multiple conditions due to operator precedence. So where the condition is true 5
is returned and 0
otherwise:
In [29]: df['points'] = np.where( ( (df['gender'] == 'male') & (df['pet1'] == df['pet2'] ) ) | ( (df['gender'] == 'female') & (df['pet1'].isin(['cat','dog'] ) ) ), 5, 0) df Out[29]: gender pet1 pet2 points 0 male dog dog 5 1 male cat cat 5 2 male dog cat 0 3 female cat squirrel 5 4 female dog dog 5 5 female squirrel cat 0 6 squirrel dog cat 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With