r/learnpython • u/blackgarliccookie • 4h ago
Turning 19 boolean variables into 1.
I'm back again, hopefully with clearer information.
I have 19 boolean variables. Each one is labeled with the date of an election. Inside the variable has true/false. True is voted, False is didn't vote.
I want to create a voting history variable that has a few options.
Never voted, if all 19 are false.
Voted in the past 5 years, if any of the date variables in the past 5 years, aside from the recent midterm, have a vote.
Voted in the past 10 years, if no votes were cast in the past 5 but there were votes in the past 10.
And recently registered, which would involve the registration_dt variable which I feel more confident being able to implement.
I just really don't know how to turn these into one variable without losing information. Thanks so much :)
3
u/white-9igga 4h ago
you wont actually lose info here, because your categories are in an order of priority (never voted, then voted in last 5, then last 10 and so on) so the first one that matches wins and the rest dont matter.
easiest way, split your 19 columns into two lists by date, one for the last 5 years (leaving out the recent midterm like you said) and one for the last 10. then make a few true/false checks, like voted5 = df[last5cols].any(axis=1), voted10 = df[last10cols].any(axis=1), never = not df[allcols].any(axis=1). then use np.select with the conditions in order (never first, then voted5, then voted10) and a default for anything else, and it gives you one column with the label for each row.
the recently registered one you can do the same way, just add it as another condition and decide where it sits in the order, since someone can be registered recently and also never voted, so pick which label should win :)
2
u/FainourSpreitzer52 3h ago
are these actually 19 standalone variables in a script, or columns in a table/CSV? if theyre standalone variables, id throw them into a dict first so you can iterate over the dates instead of hardcoding every single check.
1
u/Penguinase 4h ago
if i understand you correctly then you could create a helper column like
last_vote_datethen derive those filters/labels from it.for example
1
u/Kevdog824_ 3h ago
I am not sure I understand your question, or the format of your data, but I will give it my best. For simplicity,I am going to assume your data is in the form of a dictionary mapping the date objects to the list of True/False values under those dates named
vote_history(kinda like the second image in your linked post). Looks like in screenshot 1 you might be using pandas so not sure this will translate but this should give you the general logic for this (assuming I actually understand your goal)1
u/4Floaters 3h ago
You are looking for a bit mask
1
u/igotshadowbaned 1h ago
True = 1 and False = 0, you can use this to your advantage
If you have a list of your voting results called "voted"
if sum(voted) == 0:
Would return true if all 19 values in the list are false
If you want to just take a look at a certain few, you can use slices
if sum(voted[-5:]) != 0:
Would return true if any of the last 5 items in the list are true
1
u/[deleted] 3h ago
[removed] — view removed comment