Pandas 如何创建21个10人4次的随机组,而不让人处于同一组

Pandas 如何创建21个10人4次的随机组,而不让人处于同一组,pandas,random,slice,shuffle,Pandas,Random,Slice,Shuffle,我对python还不熟悉,学习起来很辛苦。我有一项任务很乏味,希望你能帮助我 我正在为220人组织一个活动。全天有4项活动。这些活动由10人组成的团队(22个团队)进行。我们的目标是,对于每一项活动,团队都会发生变化。所以,我想重新安排4次团队组成。这是为了确保人们尽可能多地了解同事 在excel中,我在a列中有一个220个名字的列表。我给B列中的每个名字(1-220)分配了一个数字。我随机导入了模块并输入了熊猫。我的第一步是导入excel文件,将带有数字的B列转换成一个列表,并将它们洗牌 走了

我对python还不熟悉,学习起来很辛苦。我有一项任务很乏味,希望你能帮助我

我正在为220人组织一个活动。全天有4项活动。这些活动由10人组成的团队(22个团队)进行。我们的目标是,对于每一项活动,团队都会发生变化。所以,我想重新安排4次团队组成。这是为了确保人们尽可能多地了解同事

在excel中,我在a列中有一个220个名字的列表。我给B列中的每个名字(1-220)分配了一个数字。我随机导入了模块并输入了熊猫。我的第一步是导入excel文件,将带有数字的B列转换成一个列表,并将它们洗牌

走了那一步我就迷路了。从洗牌列表中,我想创建22支球队。在第一场平局后,我想重新洗牌,再创建22支球队,但条件是在第二场比赛中,没有来自一线队的人在一起。这个过程总共重复四次

理想情况下,我希望将结果导出回excel

希望有人能帮助我

同样,我正在学习,所以请分享思考过程:)

谢谢你抽出时间

亲切问候,


首先,不要使用熊猫。这是做这项工作的错误工具。改用NumPy。下面是一个代码:

In [1]: import numpy as np

In [2]: people = np.arange(1, 221, 1)
        people
Out[2]: array([  1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,
                 14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,
                 27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,
                 40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  52,
                 53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,  65,
                 66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,
                 79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,
                 92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103, 104,
                105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
                118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
                131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
                144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
                157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
                170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,
                183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195,
                196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208,
                209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220])

In [3]: np.random.choice(people, 220, replace=False).reshape(22,10)
Out[3]: array([[ 20,  48, 164, 176, 135, 190,  67, 147, 203, 130],
               [150, 177, 171, 141, 122, 127, 202, 172,  21, 219],
               [167, 206,  84, 118, 163, 181,  34,  87, 116, 184],
               [ 33,  26,  70, 119, 129, 191, 105,  69,  86, 217],
               [ 94,  24, 146,   9,  31, 208, 179, 148,  57, 102],
               [211, 170,  65, 195, 220,  61,  88, 187,  32,   7],
               [182,  40, 144, 145, 198,  47, 193,  74,  44,  90],
               [174, 121, 216,  63,  82,  38, 201, 113,  13,  66],
               [180, 137, 214,  73,  75,  51,  80,  23,  71,  18],
               [161, 115,   3, 157,  89,  79,  29,  68, 200,   8],
               [142, 173,  98,  36, 133, 215, 138,  50,  53,   4],
               [152, 101, 139,  54,  30, 108,  49, 213, 124,  83],
               [ 76,  17,  64,  10,  56,  22, 128, 153, 158, 140],
               [131,  11,  45, 192,  92, 166,  60,  37,  12, 156],
               [104,  25,   6, 205, 212, 197,  77,  46, 199,  96],
               [ 59,  19, 112, 132, 126, 159, 151, 207,  85, 109],
               [ 42,  55, 204, 188, 185,  35,  62,  41,  27, 178],
               [ 14, 194,   2, 186, 143,  78, 134, 103, 106, 110],
               [100,  91,  99, 111,  72,  58,  15, 120, 136,  97],
               [ 39,  81, 123, 149, 165, 169, 209, 175,   1, 117],
               [189,  28,  95, 114, 162, 160, 196,   5,  93, 154],
               [210,  16, 168, 218,  43, 107, 155, 125,  52, 183]])
请注意,
Out[3]
是一个22行10列的矩阵(22个团队,每个团队10人)。您只需在[3]中的
中运行代码,运行次数与您的任务相同。所以基本上:

In [4]: tasks = {} # Tasks is a dictionary with task number as key, and teams array as value.
        for i in np.range(1, 5, 1):
            tasks['task_' + str(i)] = np.random.choice(people, 220, replace=False).reshape(22,10)
现在,您可以将存储在
tasks
中的结果返回到Excel文件中,并从中继续。如果您有任何顾虑,请告诉我


如果您坚持使用Pandas,并且已经在
df
中加载了数据,那么您可以使用Pandas返回每个团队、每个任务的球员姓名。此代码应足以满足您的需要:

df.set_index('player_id', inplace=True) # Which is actually pointless because
# Pandas will automatically create a 0 indexed index when you read your data in.
# So you don't even need any player_id column to begin with. If that is the case,
# change In [2] above to people = np.arange(0, 220, 1), or simply, np.arange(220)

tasks = {}
for i in np.arange(1, 5, 1):
    tasks['task_' + str(i)] = {}
    j = 1
    for team in np.random.choice(people, 220, replace=False).reshape(22,10):
      tasks[i]['team_' + str(j)] = df.loc[team].values
      j += 1
任务
在此代码末尾,将嵌套字典,其结构如下:

tasks = {'task_1' : {'team_1'  : [list_of_players],
                     'team_2'  : [list_of_players],
                      ...
                     'team_19' : [list_of_players],
                     'team_20' : [list_of_players]},
         'task_1' : {'team_1'  : [list_of_players],
                     'team_2'  : [list_of_players],
                      ...
                     'team_19' : [list_of_players],
                     'team_20' : [list_of_players]},
         'task_1' : {'team_1'  : [list_of_players],
                     'team_2'  : [list_of_players],
                      ...
                     'team_19' : [list_of_players],
                     'team_20' : [list_of_players]},
         'task_1' : {'team_1'  : [list_of_players],
                     'team_2'  : [list_of_players],
                      ...
                     'team_19' : [list_of_players],
                     'team_20' : [list_of_players]}}

希望这有帮助

首先,不要使用熊猫。这是做这项工作的错误工具。改用NumPy。下面是一个代码:

In [1]: import numpy as np

In [2]: people = np.arange(1, 221, 1)
        people
Out[2]: array([  1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,
                 14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,
                 27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,
                 40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  52,
                 53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,  65,
                 66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,
                 79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,
                 92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103, 104,
                105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
                118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
                131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
                144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
                157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
                170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,
                183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195,
                196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208,
                209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220])

In [3]: np.random.choice(people, 220, replace=False).reshape(22,10)
Out[3]: array([[ 20,  48, 164, 176, 135, 190,  67, 147, 203, 130],
               [150, 177, 171, 141, 122, 127, 202, 172,  21, 219],
               [167, 206,  84, 118, 163, 181,  34,  87, 116, 184],
               [ 33,  26,  70, 119, 129, 191, 105,  69,  86, 217],
               [ 94,  24, 146,   9,  31, 208, 179, 148,  57, 102],
               [211, 170,  65, 195, 220,  61,  88, 187,  32,   7],
               [182,  40, 144, 145, 198,  47, 193,  74,  44,  90],
               [174, 121, 216,  63,  82,  38, 201, 113,  13,  66],
               [180, 137, 214,  73,  75,  51,  80,  23,  71,  18],
               [161, 115,   3, 157,  89,  79,  29,  68, 200,   8],
               [142, 173,  98,  36, 133, 215, 138,  50,  53,   4],
               [152, 101, 139,  54,  30, 108,  49, 213, 124,  83],
               [ 76,  17,  64,  10,  56,  22, 128, 153, 158, 140],
               [131,  11,  45, 192,  92, 166,  60,  37,  12, 156],
               [104,  25,   6, 205, 212, 197,  77,  46, 199,  96],
               [ 59,  19, 112, 132, 126, 159, 151, 207,  85, 109],
               [ 42,  55, 204, 188, 185,  35,  62,  41,  27, 178],
               [ 14, 194,   2, 186, 143,  78, 134, 103, 106, 110],
               [100,  91,  99, 111,  72,  58,  15, 120, 136,  97],
               [ 39,  81, 123, 149, 165, 169, 209, 175,   1, 117],
               [189,  28,  95, 114, 162, 160, 196,   5,  93, 154],
               [210,  16, 168, 218,  43, 107, 155, 125,  52, 183]])
请注意,
Out[3]
是一个22行10列的矩阵(22个团队,每个团队10人)。您只需在[3]
中的
中运行代码,运行次数与您的任务相同。所以基本上:

In [4]: tasks = {} # Tasks is a dictionary with task number as key, and teams array as value.
        for i in np.range(1, 5, 1):
            tasks['task_' + str(i)] = np.random.choice(people, 220, replace=False).reshape(22,10)
现在,您可以将存储在
tasks
中的结果返回到Excel文件中,并从中继续。如果您有任何顾虑,请告诉我


如果您坚持使用Pandas,并且已经在
df
中加载了数据,那么您可以使用Pandas返回每个团队、每个任务的球员姓名。此代码应足以满足您的需要:

df.set_index('player_id', inplace=True) # Which is actually pointless because
# Pandas will automatically create a 0 indexed index when you read your data in.
# So you don't even need any player_id column to begin with. If that is the case,
# change In [2] above to people = np.arange(0, 220, 1), or simply, np.arange(220)

tasks = {}
for i in np.arange(1, 5, 1):
    tasks['task_' + str(i)] = {}
    j = 1
    for team in np.random.choice(people, 220, replace=False).reshape(22,10):
      tasks[i]['team_' + str(j)] = df.loc[team].values
      j += 1
任务
在此代码末尾,将嵌套字典,其结构如下:

tasks = {'task_1' : {'team_1'  : [list_of_players],
                     'team_2'  : [list_of_players],
                      ...
                     'team_19' : [list_of_players],
                     'team_20' : [list_of_players]},
         'task_1' : {'team_1'  : [list_of_players],
                     'team_2'  : [list_of_players],
                      ...
                     'team_19' : [list_of_players],
                     'team_20' : [list_of_players]},
         'task_1' : {'team_1'  : [list_of_players],
                     'team_2'  : [list_of_players],
                      ...
                     'team_19' : [list_of_players],
                     'team_20' : [list_of_players]},
         'task_1' : {'team_1'  : [list_of_players],
                     'team_2'  : [list_of_players],
                      ...
                     'team_19' : [list_of_players],
                     'team_20' : [list_of_players]}}

希望这有帮助

这里有一个蛮力算法——它依赖于随机选择,如果失败,它只需再试一次

根本没有使用熊猫,只是普通的列表和集合

工作原理:
  • 挑选一个人加入团队
  • 如果他已经遇到了什么人,就把他带回去再找一个人
  • 否则,让他加入团队并更新整个团队的
    met
    变量
  • 有时候,碰巧的是,剩下的每个人都已经遇到了团队中的某个人——在这种情况下,就不要麻烦了,提出一个例外

来自随机导入选择
班长:
定义初始化(self,name):
self.name=名称
self.met=set()
定义报告(自我):
返回self.name
类TooManyIteration(异常):
通过
游戏=4
团队=22
团队规模=10
def挑选团队:
s_uuz=s.copy()
团队=[set()用于范围内的团队]
对于团队中的t:
c=0
而len(t)<团队规模:
p=选择(元组(s_2;))
移除(p)
如果全部(p不在x中,x在t中满足):
t、 加(p)
其他:
补充(p)
c+=1
如果c>len(s):
#拣不到
提高Tooman迭代次数()
对于团队中的t:
对于t中的x:
x、 气象更新(t)
返回队
s=范围(220)内i的设置(人(str(i))
#s=设置(姓名列表中姓名的人员(姓名))
游戏=[]
而len(游戏)<游戏:
尝试:
游戏。附加(挑选团队)
除Tooman迭代外:
持续
印刷(游戏)

幸运的是,你只需要四个游戏,我想它的复杂度是指数级的。

这里有一个蛮力算法-它取决于随机选择,如果失败,它只需再试一次

根本没有使用熊猫,只是普通的列表和集合

工作原理:
  • 挑选一个人加入团队
  • 如果他已经遇到了什么人,就把他带回去再找一个人
  • 否则,让他加入团队并更新整个团队的
    met
    变量
  • 有时候,碰巧的是,剩下的每个人都已经遇到了团队中的某个人——在这种情况下,就不要麻烦了,提出一个例外

来自随机导入选择
班长:
定义初始化(self,name):
self.name=名称
self.met=set()
定义报告(自我):
返回self.name
类TooManyIteration(异常):
通过
游戏=4
团队=22
团队规模=10
def挑选团队:
s_uuz=s.copy()
团队=[set()用于范围内的团队]
对于团队中的t:
c=0
而len(t)<团队规模:
p=选择(元组(s_2;))
移除(p)
如果全部(p不在x中,x在t中满足):
t、 加(p)
其他:
补充(p)
c+=1
如果c>len(s):
#拣不到
提高Tooman迭代次数()
对于