[英]Randomly selecting n rows from pandas dataframe and moving them to new df without repetition
I have a dataframe of 140 students and I need to randomly assign each to one of 5 TAs(graders).我有一个 140 名学生的 dataframe,我需要将每个学生随机分配给 5 个 TA(评分者)中的一个。
an example is一个例子是
graders = ['K', 'M', ]
df = pd.DataFrame({
'First name': ['John', 'Paul', 'George','Ringo'],
'Last name':['Lennon', 'McCartney', 'Harrison', 'Star'],
})
df['Grader'] = ''
How would I randomly assign the Grader 'K' to 3 of the students and then assign the rest to 'M', making sure that a student cant end up in both groups.我如何将评分者“K”随机分配给 3 名学生,然后将 rest 分配给“M”,以确保学生不能同时进入两组。
I have gone through a number of the answers on here but none have clarified it for me, any help would be much appreciated.我已经在这里阅读了许多答案,但没有人为我澄清,任何帮助将不胜感激。
You could assign a random number 1-5 and then map those numbers to TAs.您可以分配一个随机数 1-5,然后将 map 这些数字分配给 TA。 This won't guarantee that each TA gets 1/5 of the total, though.
但是,这并不能保证每个 TA 获得总数的 1/5。
import pandas as pd
import numpy as np
df['id'] = np.random.randint(1,6, df.shape[0]) # make a new column of random ints 1-5
df['Grader'] = df['id'].map({1:'a',2:'b',3:'c',4:'d',5:'e'}) # turns 1 to 'a', 2 to 'b', etc. Change this to your actual TAs.
In [1291]: df['Grader'] = 'M' # Assign `M` to all the students at first
In [1299]: df.loc[df.sample(n=3).index, 'Grader'] = 'K' # Randomly choose 3 students and change their Grader to 'K'
In [1300]: df
Out[1300]:
First name Last name Grader
0 John Lennon K
1 Paul McCartney M
2 George Harrison K
3 Ringo Star K
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.