基于另一个表中的多列在一个表中创建列[python]

2024-06-26 17:52:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个csv表,其中有关于我所有订单的信息。现在我想把这些东西卖掉,但我想根据物品的价格加上额外的附加费。我创建了一个包含附加费的新表,其中有名为“from”和“to”的列,其中我必须比较商品价格,然后在销售价格中包含正确的附加费。你知道吗

但我做不到。我尝试了不同的方法,但似乎没有一种有效。任何帮助都很好:)

我的桌子是这样的:

    OrderNo      NetPerPiece costsDividedPerOrder  HandlingPerPiece

0  7027514279        44.24     0.008007          0.354232

1  7027514279        15.93     0.008007          0.127552

2  7027514279        15.93     0.008007          0.127552

3  7027514279        15.93     0.008007          0.127552

4  7027514279        15.93     0.008007          0.127552
surcharges = {'surcharge': [0.35, 0.25, 0.2, 0.15, 0.12, 0.1],
'from': [0, 20, 200, 500, 1500, 5000], 
'to' : [20, 200, 500, 1500, 5000,1000000000] }
surchargeTable = DataFrame(surcharges, columns=['surcharge', 'from', 'to'])


productsPerOrder['NetPerpieceSale'] = numpy.where(((productsPerOrder['NetPerPiece'] >= surchargeTable['from']) & (productsPerOrder['NetPerPiece'] < surchargeTable['to'])), surchargeTable['surcharge'])


#I also tried this:

for index, row in productsPerOrder.iterrows():
        if row['NetPerPiece'] >= surchargeTable['from'] & row['NetPerPiece'] < surchargeTable['to']:
                productsPerOrder.loc[index,'NerPerPieceSale'] = surchargeTable.loc[row,'NetPerPieceSale'].values(0)

我希望它看起来像这样:

 OrderNo   NetPerPiece costsDividedPerOrder  HandlingPerPiece NetPerPieceSale

0  7027514279   44.24           0.008007          0.354232    0.25

1  7027514279   15.93           0.008007          0.127552    0.35

2  7027514279   15.93           0.008007          0.127552    0.35

3  7027514279   15.93           0.008007          0.127552    0.35

4  7027514279   15.93           0.008007          0.127552    0.35

只是提醒一下,文件与项目是大得多,我只显示了头的csv列表。所以桌子的长度不同

附加费表如下所示:

 surcharge  from          to
0       0.35     0          20
1       0.25    20         200
2       0.20   200         500
3       0.15   500        1500
4       0.12  1500        5000
5       0.10  5000  1000000000

Tags: csvtofromindexrow附加费桌子orderno
3条回答

创建一个函数来计算附加费,然后使用.apply将其应用于“NetPerPiece”行。你知道吗

import pandas as pd
df = pd.read_csv('something.csv')   

def get_surcharges(x):
    to = [0, 20, 200, 500, 1500, 5000] 
    fr = [20, 200, 500, 1500, 5000,1000000000]
    surcharges = [0.35, 0.25, 0.2, 0.15, 0.12, 0.1]
    rr = list(zip(to, fr, surcharges))
    price = [r[2] for r in rr if x > r[0] and x <r[1]]
    return price[0]

df['NetPerpieceSale'] = df['NetPerPiece'].apply(lambda x: get_surcharges(x))

print(df)

这将输出:

      OrderNo  NetPerPiece  costsDividedPerOrder  HandlingPerPiece  NetPerpieceSale
0  7027514279        44.24              0.008007          0.354232             0.25
1  7027514279        15.93              0.008007          0.127552             0.35
2  7027514279        15.93              0.008007          0.127552             0.35
3  7027514279        15.93              0.008007          0.127552             0.35
4  7027514279        15.93              0.008007          0.127552             0.35

不带for循环的选项(有点冗长):

def get_surcharges(x):
    if x > 0:
        if x > 20:
            if x > 200:
                if x > 500:
                    if x > 1500:
                        if x > 5000:
                            return 0.1
                        else:
                            return 0.12
                    else:
                        return 0.15
                else:
                    return 0.2
            else:
                return 0.25
        else:
            return 0.35

只需使用NetPerPieceScale的上述计算向现有数据帧添加一列
或者,您可以将计算保存到如下数据帧:
net=pd.DataFrame(NetPerPieceScale, columns=['NetPerPieceScale '])

只需将它连接到现有的数据帧,就可以将所有内容都放在一个表中

另一种方法是使用pd.IntervalIndexmap

# Create IntervalIndex on surchageTable dataframe
surchargeTable = surchargeTable.set_index(pd.IntervalIndex.from_arrays(surchargeTable['from'],
                                                                       surchargeTable['to']))

#Use map to pd.Series created from surchargeTable IntervalIndex and surcharge column.
productsPerOrder['NetPerPieceSale'] = productsPerOrder['NetPerPiece'].map(surchargeTable['surcharge'])

productsPerOrder

输出:

      OrderNo  NetPerPiece  costsDividedPerOrder  HandlingPerPiece  NetPerPieceSale
0  7027514279        44.24              0.008007          0.354232             0.25
1  7027514279        15.93              0.008007          0.127552             0.35
2  7027514279        15.93              0.008007          0.127552             0.35
3  7027514279        15.93              0.008007          0.127552             0.35
4  7027514279        15.93              0.008007          0.127552             0.35

相关问题 更多 >