简体   繁体   中英

Pivot pandas data frame and sum selective column

I have a pandas dataframe as below:

    SALES_DATE      CATEGORY_NAME  ITEM_PRICE  SUM_QUANTITY  TOTAL_SALES
0   2019-10-04  Bathroom Supplies        7.49         100.0       749.00
1   2019-10-04            Produce        2.59          71.0       183.89
2   2019-10-04              Dairy        5.29         558.0      2951.82

I want to pivot such that the row become the column headers and sum the total_sales by date

SALES_DATE Bathroom Supplies Produce Dairy   TOTAL_SALES
2019-05-20 100.0.            71.0    2951.82 3883

I tried doing but its not giving me the right return

basket_NY = pd.pivot_table(basket_NY, index=['SALES_DATE'],values=["SUM_QUANTITY","TOTAL_SALES"],columns=['CATEGORY_NAME'],aggfunc=np.sum)

I believe you need to split it up into a pivot and an aggregate sum then join those on your "index":

import pandas as pd

df = pd.DataFrame({
    "SALES_DATE": ["2019-10-04", "2019-10-04", "2019-10-04"],
    "CATEGORY_NAME": ["Bathroom Supplies", "Produce", "Dairy"],
    "ITEM_PRICE": [7.49, 2.59, 5.29],
    "SUM_QUANTITY": [100, 71, 558],
    "TOTAL_SALES": [749, 183.89, 2951.82]})

# Get pivot values
pivot = pd.pivot_table(
    df, index=["SALES_DATE"], values=["SUM_QUANTITY"],
    columns=["CATEGORY_NAME"]) 
pivot.columns = [i[1] for i in pivot.columns]
# Join with sum of total sales
result = pivot.join(
    df.groupby("SALES_DATE")["TOTAL_SALES"].sum()).reset_index()

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