简体   繁体   中英

How to make sum of Row which are flitered with specific column values in Pandas?

i have a dataframe like below. Trying to sum Week 7 and Week 8 SalesQuantity values for all the regarding productCodes[1-317], and update their week 7 rows Sales Quantity as a new value. And deleting their Week 8 rows from Dataframe.

Week column range is [7-26] and all of the weeks include [1-317] product code cause of the original data is before group by [Week,ProductCode]

 Week  ProductCode  SalesQuantity
  7            1      49.285714
  7            2      36.714286
  7            3      33.285714
  7            4      36.857143
  7            5      42.714286
 ...          ...            ...
  8            3      61.000000
 26          314       4.285714
 26          315       3.571429
 26          316       6.142857
 26          317       3.285714

Example Result: From the above table, adding week 7+8 SalesQuantities for product code 3: 61.000+33.285714= 94.285.714 new SalesQuantity updated value for week 7 is founded for ProductCode 3. After that, need delete Week 8 row for ProductCode 3.

How to automate it for all of the ProductCode[1-317]?

Thanks

Use the `groupby()' method:

sumSales = data[['productCode', 'SalesQuality']].groupby('ProductCode').sum()

This creates a new DataFrame, with the sum of SalesQuality, indexed with the product code. The data[['productCode', 'SalesQuality']] part creates a sub-selection of the original data frame, otherwise the weeks also get summed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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