r/learnpython • u/blackgarliccookie • 28m 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.
•
u/Own_Letterhead2853 6m ago
are you trying to keep the other three combinations separate or lump them all into an 'other' category? if you want all four combos to have their own label, you can honestly just concatenate the two string columns together with a space in between and then rename the specific one you care about
•
u/stepback269 6m ago
So you would be doing a very primitive object oriented thing by creating a bunch of lists
The 'constructor' function for doing this might look like this:
def instantiate_this_person(name = 'string', green_status = False, purple_status = False):
... object = [name, green_status, purple_status]
... return object
person_1 = instantiate_this_person("Adam". 0, 1)
person_2 = instantiate_this_person("Bob". 1, 0)
person_3 = instantiate_this_person("Carl". 1, 1)
Now use slicing to compare the 'attributes' of each of the instantiated persons
Good luck
•
u/aiuniverse-3p3_here 19m 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