Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将数据帧拆分为具有唯一值的块_Python_Pandas_Dataframe_Data Wrangling - Fatal编程技术网

Python 将数据帧拆分为具有唯一值的块

Python 将数据帧拆分为具有唯一值的块,python,pandas,dataframe,data-wrangling,Python,Pandas,Dataframe,Data Wrangling,我有两个组(a和B)的数据帧,在这些组中有6个子组(a,B,c,d,e和f)。示例数据如下: index group subgroup value 0 A a 1 1 A b 1 2 A c 1 3 A d 1 4 A e 1 5 A

我有两个组(
a
B
)的数据帧,在这些组中有6个子组(
a
B
c
d
e
f
)。示例数据如下:

index   group    subgroup    value
0       A        a           1
1       A        b           1
2       A        c           1
3       A        d           1
4       A        e           1
5       A        f           1
6       B        a           1
7       B        b           1
8       B        c           1
9       B        d           1
10      B        e           1
11      B        f           1
...     ...      ...         ...
虽然我在这里只列出了12行,但实际数据集中有300行。我试图将数据帧随机分成50批,共6个值。重要的是,每个批次应具有每个子组中的1个,并且组的分布大致相等

期望输出:

index   group    subgroup    batch
0       A        a           1
1       A        b           1
2       A        c           1
3       B        d           1
4       B        e           1
5       B        f           1
6       A        d           2
7       A        e           2
8       A        f           2
9       B        a           2
10      B        b           2
11      B        c           2
...     ...      ...         ...

在我的数据集中,有150个
A
和150个
B
,但不幸的是,没有相同数量的子组(例如,我有25个
A
s,27个
B
s,23个
c
s等,包含
A
),因此如果批次1-48包含唯一的子组(即每个A-f中的1个),则会是首选的,但是,第49批和第50批的剩菜没有被平均分配——我需要在事后手动随机分配!最重要的是,一个批次中每个子组都有一个,但一个批次中正好有3个
a
s和3个
B
s则不那么重要。谢谢大家!

对于批次1-48,此解决方案从每个
子组
中仅挑选一个元素。批次49-50随机抽取。不考虑
A
s和
B
s中的数字

逻辑
  • 通过对每个子组的索引执行随机排列来洗牌每个子组
  • 每个子组的第一个元素构成第一批,第二个元素构成第二批,以此类推
  • 代码 输出 常规批次 我们可以看到,在每个批次中,每个子组确实有一个元素

    print(df.sort_values(["batch", "subgroup"]).head(13))
    
          group subgroup  value  batch
    index                             
    48        A        a    480      1
    13        A        b    130      1
    134       A        c   1340      1
    171       B        d   1710      1
    262       B        e   2620      1
    5         A        f     50      1
    240       B        a   2400      2
    291       B        b   2910      2
    152       B        c   1520      2
    93        A        d    930      2
    136       A        e   1360      2
    59        A        f    590      2
    24        A        a    240      3
    
    其余的
    -谢谢你的评论代码!这对于将独特的子组分为批非常有效,但您能否就如何在每个批中获得大致相等的
    group
    分布给出建议?不幸的是,我现在想不出一种方法,否则我已经这样做了。我所能想到的每一种策略都无法阻止极端情况的发生。例如,(1)某个
    (组、子组)
    的早期耗尽,或(2)在所有其他
    子组
    耗尽后,某个
    子组
    中仍有太多未勾选的条目。我甚至不确定这是否能在多项式时间内完成。因此,我首先选择了最低要求。
    print(df.sort_values(["batch", "subgroup"]).head(13))
    
          group subgroup  value  batch
    index                             
    48        A        a    480      1
    13        A        b    130      1
    134       A        c   1340      1
    171       B        d   1710      1
    262       B        e   2620      1
    5         A        f     50      1
    240       B        a   2400      2
    291       B        b   2910      2
    152       B        c   1520      2
    93        A        d    930      2
    136       A        e   1360      2
    59        A        f    590      2
    24        A        a    240      3
    
    print(df.sort_values(["batch", "subgroup"]).tail(13))
    
          group subgroup  value  batch
    index                             
    29        A        f    290     48
    120       A        a   1200     49
    222       B        a   2220     49
    276       B        a   2760     49
    61        A        b    610     49
    133       A        b   1330     49
    289       B        b   2890     49
    98        A        c    980     50
    206       B        c   2060     50
    45        A        d    450     50
    295       B        d   2950     50
    166       B        e   1660     50
    233       B        f   2330     50