Python 熊猫:基于三个不同的其他列值求和和和聚合列值

Python 熊猫:基于三个不同的其他列值求和和和聚合列值,python,pandas,Python,Pandas,我正在做这项工作。为了为未来销售预测的神经网络准备数据,我需要汇总每天特定产品的销售数量。我现在的代码如下所示 import pandas as pd df = pd.read_csv('data_date.csv', encoding='cp1252') df = df.drop(df.columns[[0,3,7,8]], axis=1) print(df.head(5)) InvoiceNo StockCode Quantity InvoiceDate UnitPrice 0

我正在做这项工作。为了为未来销售预测的神经网络准备数据,我需要汇总每天特定产品的销售数量。我现在的代码如下所示

import pandas as pd

df = pd.read_csv('data_date.csv', encoding='cp1252')
df = df.drop(df.columns[[0,3,7,8]], axis=1)
print(df.head(5))
InvoiceNo StockCode  Quantity InvoiceDate  UnitPrice
0    536365    85123A         6  2010-12-01       2.55
1    536365     71053         6  2010-12-01       3.39
2    536365    84406B         8  2010-12-01       2.75
3    536365    84029G         6  2010-12-01       3.39
4    536365    84029E         6  2010-12-01       3.39
这将输出以下内容

import pandas as pd

df = pd.read_csv('data_date.csv', encoding='cp1252')
df = df.drop(df.columns[[0,3,7,8]], axis=1)
print(df.head(5))
InvoiceNo StockCode  Quantity InvoiceDate  UnitPrice
0    536365    85123A         6  2010-12-01       2.55
1    536365     71053         6  2010-12-01       3.39
2    536365    84406B         8  2010-12-01       2.75
3    536365    84029G         6  2010-12-01       3.39
4    536365    84029E         6  2010-12-01       3.39
现在,我的目标是汇总,例如,2010-12-01《发票日期》InvoideDate上的
StockCode
项目71053的
数量。但这只是一个例子,我需要的是一个概述,每个股票代码每天售出多少个商品

我尝试了大量的
groupy
-操作并找到了答案,这使我非常接近所需的输出

df["Quantity"] = df.groupby(["InvoiceDate", "StockCode"])["Quantity"].transform(sum)
print(df.head(5))
InvoiceNo StockCode  Quantity InvoiceDate  UnitPrice
0    536365    85123A       454  2010-12-01       2.55
1    536365     71053        33  2010-12-01       3.39
2    536365    84406B        40  2010-12-01       2.75
3    536365    84029G        59  2010-12-01       3.39
4    536365    84029E       551  2010-12-01       3.39
这给了我以下输出

df["Quantity"] = df.groupby(["InvoiceDate", "StockCode"])["Quantity"].transform(sum)
print(df.head(5))
InvoiceNo StockCode  Quantity InvoiceDate  UnitPrice
0    536365    85123A       454  2010-12-01       2.55
1    536365     71053        33  2010-12-01       3.39
2    536365    84406B        40  2010-12-01       2.75
3    536365    84029G        59  2010-12-01       3.39
4    536365    84029E       551  2010-12-01       3.39
看起来已经不错了,但是当我用一个特定的
StockCode
进行测试时,它仍然将相同的
数量
放在不同的行上,而不是真正地聚合它。见下面的例子

print(df.loc[df['StockCode']=='22632'])
输出

InvoiceNo StockCode  Quantity InvoiceDate  UnitPrice
8         536366     22632       233  2010-12-01       1.85
47        536372     22632       233  2010-12-01       1.85
84        536377     22632       233  2010-12-01       1.85
257       536394     22632       233  2010-12-01       1.85
304       536398     22632       233  2010-12-01       2.10
315       536399     22632       233  2010-12-01       1.85
433       536407     22632       233  2010-12-01       1.85
664       536415     22632       233  2010-12-01       2.10
704       536423     22632       233  2010-12-01       2.10
879       536477     22632       233  2010-12-01       2.10
952       536520     22632       233  2010-12-01       2.10
1029      536522     22632       233  2010-12-01       2.10
1066      536522     22632       233  2010-12-01       2.10
1260      536532     22632       233  2010-12-01       2.10
1399      536539     22632       233  2010-12-01       2.10
1441     C536543     22632       233  2010-12-01       2.10
1628      536544     22632       233  2010-12-01       4.21
2139      536561     22632       233  2010-12-01       2.10
2183      536567     22632       233  2010-12-01       2.10
2776      536592     22632       233  2010-12-01       4.21
3130      536601     22632       169  2010-12-02       1.85
那么,我如何处理数据,使其在一行上显示233的数量,而不考虑
单价
发票号

像这样

InvoiceNo StockCode  Quantity InvoiceDate  UnitPrice
    8         536366     22632       233  2010-12-01       1.85
    3130      536601     22632       169  2010-12-02       1.85
此外,我还想知道是否有办法按照
StockCode
InvoiceDate
以及不同的
单价对销售额进行分组

提前谢谢你

我认为,如果希望每列只显示第一行,则需要
InvoiceDate
StockCode

df["Quantity"] = df.groupby(["InvoiceDate", "StockCode"])["Quantity"].transform(sum)
df11 = df.drop_duplicates(['InvoiceDate','StockCode'])
print (df11)
     InvoiceNo  StockCode  Quantity InvoiceDate  UnitPrice
8       536366      22632       233  2010-12-01       1.85
3130    536601      22632       169  2010-12-02       1.85
与聚合相同的解决方案是首先指定聚合函数

df11 = (df.groupby(["InvoiceDate", "StockCode"], as_index=False)
         .agg({'Quantity': 'sum', 'UnitPrice':'first', 'InvoiceNo': 'first'})
         .reindex(columns=df.columns))
print (df11)
  InvoiceNo  StockCode  Quantity InvoiceDate  UnitPrice
0    536366      22632      4660  2010-12-01       1.85
1    536601      22632       169  2010-12-02       1.85
旧答案:

df1 = df.groupby(["InvoiceDate", "StockCode"], as_index=False)["Quantity"].sum()
print (df1)
  InvoiceDate StockCode  Quantity
0  2010-12-01     71053         6
1  2010-12-01    84029E         6
2  2010-12-01    84029G         6
3  2010-12-01    84406B         8
4  2010-12-01    85123A         6
但如果需要输出中的所有列,请将它们添加到
groupby
或为每列指定聚合函数:

df2 = (df.groupby(["InvoiceNo","InvoiceDate", "StockCode"], as_index=False)
               ['Quantity','UnitPrice'].sum())
print (df2)
   InvoiceNo InvoiceDate StockCode  Quantity  UnitPrice
0     536365  2010-12-01     71053         6       3.39
1     536365  2010-12-01    84029E         6       3.39
2     536365  2010-12-01    84029G         6       3.39
3     536365  2010-12-01    84406B         8       2.75
4     536365  2010-12-01    85123A         6       2.55
或为每个列指定聚合函数,如:

df2 = (df.groupby(["InvoiceDate", "StockCode"], as_index=False)
         .agg({'Quantity': 'sum', 'UnitPrice':'mean', 'InvoiceNo': 'first'}))
print (df2)
  InvoiceDate StockCode  Quantity  UnitPrice  InvoiceNo
0  2010-12-01     71053         6       3.39     536365
1  2010-12-01    84029E         6       3.39     536365
2  2010-12-01    84029G         6       3.39     536365
3  2010-12-01    84406B         8       2.75     536365
4  2010-12-01    85123A         6       2.55     536365

如果我运行上述代码,
数量
将替换为
发票编号
。我对自己函数的输出非常满意,它在最后使用了
.transform(sum)
,但我只需要一种方法来显示同一
InvoiceDate
StockCode
的一行而不是多行。是的,这看起来不错!你知道如何使用平均价格,就像你以前试图展示的那样?那么,我可以应用
.agg
我的代码吗?或者我需要重写它吗?@Cut7er-我认为旧的解决方案应该会有帮助,所以也添加到了答案中;)好的,很好,我会试着玩一下。但是删除重复是第一个好主意,我已经实现了:-)@Cut7er-它与
(df.groupby([“InvoiceDate”,“StockCode]”,as_index=False)相同。agg({“Quantity':“sum”,“UnitPrice':“first”,“InvoiceNo':“first”)。reindex(columns=df.columns))
-聚合另一列的第一个值-添加到答案中。