简体   繁体   中英

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).

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.

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. This won't guarantee that each TA gets 1/5 of the total, though.

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.

Use df.sample :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM