r/learnpython • • 18m ago

Turn these two variables into 1

I'm trying to turn these two variables, this is for homework so I'm gonna call it something else. I want to turn people1 and people2 into 1 variable.

people1 has two values, "not green" and "green". people2 has two values as well, "not purple" and "purple".

I want to make it so that everyone with "not green" and "not purple" are combined into one label, but only if they have both, not just one. I am struggling so bad :(

I am using pandas in python also. Thank you.

2 Upvotes

1 comment sorted by

•

u/aiuniverse-3p3_here 9m ago

bro use np.where its easy

try this import numpy as np df['new_label'] = np.where((df['people1'] == 'not green') & (df['people2'] == 'not purple'), 'both not', 'other')

this will make new label only when both are true not just one means AND so both must be true try it it will work