按类型和勺子拆分订单数量

时间:2017-10-06 02:44:56

标签: python pandas

拥有以下数据框:

**ASK**
Date        Type    Scoop   Qty
-------------------------------
6-Oct-17    A       Single  80
10-Oct-17   B       Triple  90
9-Oct-17    D       Double  40
10-Oct-17   C       Double  20
10-Oct-17   B       Triple  90
9-Oct-17    A       Single  30

**ICECREAMTYPE**
Type    Scoop   Flavour1    Flavour2    Flavour3    Scoops/Tub  Proportion
---------------------------------------------------------------------------
A       Single  Strawberry                                  4       0.25
C       Double  Banana      Lemon                           2       0.25
B       Triple  Vanilla     Lemon       Mint                3       0.11
D       Double  Chocolate   Vanilla                         5       0.10

根据比例,寻找一个有助于确定要订购多少个冰淇淋桶的蟒蛇逻辑。

所需的输出是:

**ORDERTUBS**
Date        Type    Scoop       Flavour     Qty
------------------------------------------------
6-Oct-17    A       Flavour1    Strawberry  20
10-Oct-17   B       Flavour1    Vanilla     10
10-Oct-17   B       Flavour2    Lemon       10
10-Oct-17   B       Flavour3    Mint        10
9-Oct-17    D       Flavour1    Chocolate   4
9-Oct-17    D       Flavour2    Vanilla     4
10-Oct-17   C       Flavour1    Banana      5
10-Oct-17   C       Flavour2    Lemon       5
10-Oct-17   B       Flavour1    Vanilla     10
10-Oct-17   B       Flavour2    Lemon       10
10-Oct-17   B       Flavour3    Mint        10
9-Oct-17    A       Flavour1    Strawberry  7.5

谢谢!

1 个答案:

答案 0 :(得分:2)

这是你想要的吗?我打破了步骤

import pandas as pd
Target=df.merge(df1.drop('Scoop',1),on='Type',how='left')
Target=Target.set_index(['Date','Type','Scoop','Qty','Scoops/Tub','Proportion']).stack().reset_index()
Target['Qty']=Target['Qty']*Target['Proportion']
Target.drop(['Scoops/Tub','Proportion','Scoop'],1).rename(columns={'level_6':'Scoop',0:'Flavour'})

Out[49]: 
         Date Type   Qty     Scoop     Flavour
0    6-Oct-17    A  20.0  Flavour1  Strawberry
1   10-Oct-17    B   9.9  Flavour1     Vanilla
2   10-Oct-17    B   9.9  Flavour2       Lemon
3   10-Oct-17    B   9.9  Flavour3        Mint
4    9-Oct-17    D   4.0  Flavour1   Chocolate
5    9-Oct-17    D   4.0  Flavour2     Vanilla
6   10-Oct-17    C   5.0  Flavour1      Banana
7   10-Oct-17    C   5.0  Flavour2       Lemon
8   10-Oct-17    B   9.9  Flavour1     Vanilla
9   10-Oct-17    B   9.9  Flavour2       Lemon
10  10-Oct-17    B   9.9  Flavour3        Mint
11   9-Oct-17    A   7.5  Flavour1  Strawberry

数据输入:

df
Out[50]: 
        Date Type   Scoop  Qty
0   6-Oct-17    A  Single   80
1  10-Oct-17    B  Triple   90
2   9-Oct-17    D  Double   40
3  10-Oct-17    C  Double   20
4  10-Oct-17    B  Triple   90
5   9-Oct-17    A  Single   30

df1
Out[51]: 
  Type   Scoop    Flavour1 Flavour2 Flavour3  Scoops/Tub  Proportion
0    A  Single  Strawberry      NaN      NaN           4        0.25
1    C  Double      Banana    Lemon      NaN           2        0.25
2    B  Triple     Vanilla    Lemon     Mint           3        0.11
3    D  Double   Chocolate  Vanilla      NaN           5        0.10