简体   繁体   English

如何合并单个订单的值?

[英]How do I combine the values of a single order?

I cannot figure out how to combine rows with the same productcode together to sum their total order values (quantityordered * priceeach). 我无法弄清楚如何将具有相同产品代码的行组合在一起以求和它们的总订单值(quantityordered * priceeach)。 For example: 例如:

orderdetails_df
Out[24]: 
      ordernumber productcode  quantityordered  priceeach  orderlinenumber
0           10100    S18_1749               30     171.70                3
1           10100    S18_2248               50      67.80                2
2           10100    S18_4409               22      86.51                4
3           10100    S24_3969               49      34.47                1
4           10101    S18_2325               25     151.28                4
           ...         ...              ...        ...              ...
2991        10425    S24_2300               49     112.46                9
2992        10425    S24_2840               31      33.24                5
2993        10425    S32_1268               41      86.68               11
2994        10425    S32_2509               11      43.83                6
2995        10425    S50_1392               18     105.33                

I need to combine all ordernumbers 10100 and their corresponding price values (30 * 171.70, etc.) to get the total value of the entire order. 我需要将所有订单号10100及其相应的价格值(30 * 171.70,等等)结合起来,以获得整个订单的总价值。 Is there some code that will combine all like-wise ordernumbers and output their total values? 是否有一些代码可以组合所有相同的订单号并输出其总价值?

Any help is greatly appreciated. 任何帮助是极大的赞赏。

You need to compute order value and then using groupby , sum the order value for each order. 您需要计算订单价值,然后使用groupby ,将每个订单的订单价值相加。

df['order_value'] = df['quantityordered'] * df['priceeach']
df.groupby('ordernumber')['order_value'].sum().reset_index(name='total_order_value')

Output 产量

ordernumber total_order_value
0   10100   12133.25
1   10101   3782.00
2   10425   10576.99

Let us do these things separately: 让我们分别做这些事情:

  1. first we create a column that calculates the price of the quantity; 首先,我们创建一个计算数量价格的列; and
  2. next we sum up the ordernumbers. 接下来,我们总结订单号。

We can do that by: 我们可以这样做:

df['pricequantity'] = df['priceeach'] * df['quantityordered']

then we can sum up the prices with: 然后我们可以总结出价格:

df.groupby('ordernumber')['pricequantity'].sum()

For the given sample data (except the last line that was missing an orderlinenumber ), we get: 对于给定的样本数据(最后一行缺少orderlinenumber除外),我们得到:

>>> df.groupby('ordernumber')['pricequantity'].sum()
ordernumber
10100    12133.25
10101     3782.00
10425    10576.99
Name: pricequantity, dtype: float64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM