【问题标题】:Combine data to create new data randomly on conditions组合数据以根据条件随机创建新数据
【发布时间】:2016-07-14 02:15:05
【问题描述】:

我遇到了以下我想在python中解决的问题。我想将零件随机分配给具有一定容量的某些容器。这是一个带有虚拟数据框(带有熊猫)的示例,以显示我想要实现的目标:

dfA =
   Car Container  Capcity_container Container_type
0  CAR1       E-1                  1              E
1  CAR1       A-2                  2              A
2  CAR1       B-2                  1              B
3  CAR1       A-6                  2              A
4  CAR2       B-4                  1              B
5  CAR2       A-1                  4              A
6  CAR2       B-5                  1              B
7  CAR3       C-2                  2              C
8  CAR3       B-8                  1              B
9  CAR3       B-3                  2              B

dfB =
      Part   Car Container_Type
8    Part9  CAR2              B
0    Part1  CAR1              A
1    Part2  CAR1              A
2    Part3  CAR1              B
3    Part4  CAR1              E
9   Part10  CAR1              A
12  Part13  CAR1              A
4    Part5  CAR2              A
5    Part6  CAR2              A
6    Part7  CAR2              A
13  Part14  CAR2              B
7    Part8  CAR3              B
10  Part11  CAR3              B
11  Part12  CAR3              B

在dfA中,它知道哪辆车包含什么时间的具有指定容量的容器。

在 dfB 中,知道哪一部分需要在哪辆车和容器类型中。 汽车所有部件的总和与 dfA 中容器的容量总和相同。

我的目标:我想将零件随机“分配”到具有正确类型的容器中。容器“满”后,其余部分应分配给具有正确类型的另一个容器。理想情况下,它会返回如下内容:

result =     
               Part   Car Container_Type Container_assign
    0    Part1  CAR1              A              A-2
    1    Part2  CAR1              A              A-2
    2    Part3  CAR1              B              B-2
    3    Part4  CAR1              E              E-1
    9   Part10  CAR1              A              A-1
    12  Part13  CAR1              A              A-1
    4    Part5  CAR2              A              A-1
    5    Part6  CAR2              A              A-1
    6    Part7  CAR2              A              A-5
    8    Part9  CAR2              B              B-2
    13  Part14  CAR2              B              B-5
    7    Part8  CAR3              B              B-8
    10  Part11  CAR3              B              B-8
    11  Part12  CAR3              B              B-3

请注意,它们可以随机分配到集装箱上,只要满足容量要求并且零件在正确类型的集装箱和正确的汽车/ULD中

** 编辑 #2 ** @Colonel Beauvel:这是您的代码,我在尝试使用 try 函数后稍作调整,这对我来说是全新的。

for i, r in dfB.iterrows():
    mask = (dfA['count']!=0) & (dfA['Container_type']==r['Container_Type']) & (dfA['CAR']==r['CAR'])
    df   = dfA[mask]
    try:
        l.append(df.iloc[0]['Container'])
        dfA.ix[df.index[0],'count'] = dfA.ix[df.index[0],'count'] - 1
    except Exception as e:
        l.append('Not Assigned')

dfB['Container_assign']=l

返回这个:

      Part   CAR Container_Type Container_assign
0    Part9  CAR2              B              B-4
1    Part1  CAR1              A              A-2
2    Part2  CAR1              A              A-2
3    Part3  CAR1              B              B-2
4    Part4  CAR1              E              E-1
5   Part10  CAR1              A     Not Assigned
6   Part13  CAR1              A     Not Assigned
7    Part5  CAR2              A              A-1
8    Part6  CAR2              A              A-1
9    Part7  CAR2              A              A-1
10  Part14  CAR2              B              B-5
11   Part8  CAR3              B              B-8
12  Part11  CAR3              B              B-3
13  Part12  CAR3              B              B-3

出于示例目的,我将 A-6 的容量更改为零,以便取回 2 个未分配的零件。有效!

  Container   CAR  Capcity_container Container_type  count
0       E-1  CAR1                  1              E      0
1       A-2  CAR1                  2              A      0
2       B-2  CAR1                  1              B      0
3       A-6  CAR1                  0              A      0
4       B-4  CAR2                  1              B      0
5       A-1  CAR2                  4              A      1
6       B-5  CAR2                  1              B      0
7       C-2  CAR3                  2              C      2
8       B-8  CAR3                  1              B      0
9       B-3  CAR3                  2              B      0

我如何使用 else 或 finally 打印类似“All parts are assinged”的内容,容量满足部件数量并且所有内容都被分配,换句话说,没有错误?当我添加它时,它会为每个部分返回它。 编辑#3

我认为这可以解决问题,非常简单......

l = []
dfA['count'] = dfA['Capcity_container']
erroryesno = 'All parts are Assinged'
for i, r in dfB.iterrows():
    mask = (dfA['count']!=0) & (dfA['Container_type']==r['Container_Type']) & (dfA['CAR']==r['CAR'])
    df   = dfA[mask]
    try:
        l.append(df.iloc[0]['Container'])
        dfA.ix[df.index[0],'count'] = dfA.ix[df.index[0],'count'] - 1
    except Exception as e:
        l.append('Not Assigned')
        erroryesno = 'Some are not assinged'
print erroryesno
dfB['Container_assign']=l

【问题讨论】:

  • 只是为了让您知道您提供的示例中没有足够的 B 类型容器容量。
  • 嗨,博维尔上校,您完全正确,对不起。我制作了这个虚拟数据并对其进行了编辑,同时提出了可读性问题。我可能在那里犯了一个错误。我编辑了!谢谢你告诉我!
  • 您尝试过以下方法吗?
  • 我刚刚做了,当容量适合零件数量时,它可以完美运行。在您的代码中,您建议使用 try,除非是这种情况。我真的很喜欢它并希望实现它,以便当“没有空间”时,该部件在 Container_Assign 中得到“未分配”。今晚我深入研究了这个函数,我尝试了上面提到的代码,但不幸的是它总是在减少容器容量时停止运行(所以代码不会继续 -> IndexError:索引 0 超出轴 0 的范围,大小为 0)。我想我用上面的代码修复了它!你怎么看?
  • 确实有可能!很高兴能提供帮助。

标签: python python-2.7 pandas merge


【解决方案1】:

一种可能的解决方案是遍历dfB 行并获取dfA 中可用的第一个相应容器。因此,该容器容量减少了 1:

l = []
dfA['count'] = dfA['Capcity_container']

for i, r in dfB.iterrows():
    mask = (dfA['count']!=0) & (dfA['Container_type']==r['Container_Type']) & (dfA['car']==r['car'])
    df   = dfA[mask]
    try:
        l.append(df.iloc[0]['Container'])
    except Exception as e:
        print 'Not anymore container for this type'
        raise e
    dfA.ix[df.index[0],'count'] = dfA.ix[df.index[0],'count'] - 1

dfB['container_assign']=l

【讨论】:

  • 嗨,上校,谢谢,我将在真实数据上进行尝试。由于数据框很大,例外是一个非常好的补充!我会带着结果回来的!
  • 您好 Beavel 上校,首先非常感谢您对我的帮助!我认为您提供的解决方案几乎是正确的。在您的代码中不采取不同汽车的汽车。我试图让它在将 Car 列与 Container Column 组合在一起的键上工作,但这也不起作用。我编辑了虚拟数据,以便您可以看到目前不正确的内容,并将 ULD 的名称更改为 Car 以使虚拟数据更直观。关于如何完成的任何想法?
  • 我更新了我的解决方案。这很容易,因为您只需要在掩码中添加汽车过滤......但您的工作示例仍然会产生错误,因为没有足够的容器关于这对夫妇(汽车,container_type)
猜你喜欢
  • 2016-12-20
  • 2019-11-18
  • 2021-11-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 2021-02-21
  • 2021-06-18
相关资源
最近更新 更多