从lis创建随机分组

2024-04-25 22:36:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要把500多人的名单,然后把他们分成15人一组。分组应该是随机的,这样我们就不会以每个人的姓以“B”开头的分组结束。但我也需要尽可能平衡15国集团的性别平等。名单在a'学生.csv'具有此结构的文件:


Last, First, ID, Sport, Gender, INT
James, Frank, f99087, FOOT, m, I
Smith, Sally, f88329, SOC, f, 
Cranston, Bill, f64928, ,m,

我在寻找熊猫的解决方案,但我的编码知识有限。到目前为止,我得到的代码只是对数据进行了一点探索。你知道吗

import pandas as pd
data = pd.read_csv('students.csv', index_col='ID')
print(data)

print(data.Gender.value_counts())

Tags: 文件csviddatagender结构学生集团
2条回答

我要做的第一件事是过滤成两个列表,每个性别一个:

males = [d for d in data if d.Gender == 'm']
females = [d for d in data if d.Gender == 'f']

接下来,重新排列列表的顺序,以便更容易选择“随机”而实际上不必选择随机索引:

random.shuffle(males)
random.shuffle(females)

然后,选择元素,同时尽量与性别比例保持或多或少的一致:

# establish number of groups, and size of each group
GROUP_SIZE = 15
GROUP_NUM = math.ceil(len(data) / group_size)
# make an empty list of groups to add each group to
groups = []
while len(groups) < GROUP_NUM and (len(males) > 0 and len(females) > 0):
    # calculate the proper gender ratio, to perfectly balance this group
    num_males = len(males) / len(data) * GROUP_SIZE
    num_females = GROUP_SIZE - num_males
    # select that many people from the previously-shuffled lists
    males_in_this_group = [males.pop(0) for n in range(num_males) if len(males) > 0]
    females_in_this_group = [males.pop(0) for n in range(num_females) if len(females) > 0]
    # put those two subsets together, shuffle to make it feel more random, and add this group
    this_group = males_in_this_group + females_in_this_group
    random.shuffle(this_group)
    groups.append(this_group)

这将确保每组的性别比例尽可能与原始样本相符。最后一组当然会比其他组小,并且会包含其他组的“剩余部分”。你知道吗

使用pandas的方法意味着由15个成员组成的小组。其余的在最后一组。性别比例的准确度和随机抽样法所允许的差不多。你知道吗

import pandas as pd

df = pd.read_csv('1.csv', skipinitialspace=True) # 1.csv contains sample data from the question

# shuffle data / pandas way
df = df.sample(frac=1).reset_index(drop=True)

# group size
SIZE = 15

# create column with group number
df['group'] = df.index // SIZE

# list of groups, groups[0] is dataframe with the first group members
groups = [
    df[df['group'] == num]
    for num in range(df['group'].max() + 1)]

将数据帧保存到文件:

# one csv-file
df.to_csv('2.csv')

# many csv-files
for num, group_df in enumerate(groups, 1):
    group_df.to_csv('group_{}.csv'.format(num))

相关问题 更多 >