r/learnpython • u/blackgarliccookie • 21h 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.
Edit: I want something like this: https://www.reddit.com/u/blackgarliccookie/s/xkzPCrmyrB
13
u/zanfar 20h ago
You need to be MUCH more precise with your terminology.
What does that even mean? Just delete one, or don't create one? Do you mean you need to store both values in a third variable?
No. A variable always has one and exactly one value. Do you mean two possible values?
...
and so on.
You need to be providing something MUCH clearer, or provide code.
5
u/supercoach 20h ago
Share your code. Making people speculate is not how you get help.
2
u/SharkSymphony 20h ago
A variable cannot have two values, so your problem statement doesn't make enough sense to go off of.
But you mention pandas. Do you maybe mean that people1 and people2 are dataframes with two columns, and one of those columns has the, uh, color descriptions you mention?
Trying to give a precise description of the problem will often solve the problem all by itself!
2
u/LayotFctor 17h ago edited 17h ago
XY problem definitely. I'm certain the solution you want is not the solution you actually need either.
In the first place "people1" and "people2" is odd already. There is little reason for variables to have numbers like that.
Is what you need somewhere in here?
People are now dictionaries. I took out colors into a color variable, so that every people variable references the same catalogue of colors. Data just says whether it is or isn't that color. All numbered people variables are now just part of a list, where you can get people1 via people[1].
2
u/backfire10z 15h ago
Per your edit, the truth table is:
green and purple: both
green and not purple: green
not green and purple: not green
not green and not purple: none
Assuming clean data, you can use:
```
import numpy as np
import pandas as pd
is_green = df["green"] == "green"
is_purple = df["purple"] == "purple"
conditions = [
is_green & is_purple,
is_green & ~is_purple,
~is_green & is_purple,
~is_green & ~is_purple,
]
choices = [
"both",
"green",
"not green",
"none",
]
df["result"] = np.select(conditions, choices, default="none")
```
1
u/Own_Letterhead2853 20h 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
1
u/Correct_Rain_9438 16h ago
With pandas, np.where is the tool for this. Combine both tests into one condition with & so the new label only applies when both are true, and give it the fallback value for everything else.
1
u/LegalDebate5423 4h ago
If both columns are in the same DataFrame, use a mask for the exact pair: `mask = (df['people1'] == 'not green') & (df['people2'] == 'not purple')`. Then assign the new label with `df.loc[mask, 'label'] = 1`.
1
u/stepback269 20h 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