我在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中集装箱容量之和相同。
我的目标:我想要‘分配’部分随机的容器与正确的类型。在容器“满”之后,其余的部分应该分配给另一个具有正确type.Ideally的容器,它将返回如下内容:
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请注意,只要满足容量要求,并且部件在正确的容器类型和正确的car/ULD中,它们可以被随机分配到集装箱上,。
**编辑#2 ** @Beauvel上校:这是你的代码,我在尝试函数之后做了一些调整,这对我来说是全新的。
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我如何使用其他或最终打印的东西,如“所有的部分都是分析”,容量满足零件的数量和所有的东西被分类,换句话说,没有错误?当我添加它时,它会为每个部分返回它。编辑#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发布于 2016-03-26 12:56:45
一种可能的解决方案是迭代dfB行并获取dfA中可用的第一个相应的容器。因此,这一集装箱容量减少了一个:
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']=lhttps://stackoverflow.com/questions/36234921
复制相似问题