将总值的%插入pandas中的数据透视表

问题描述 投票:0回答:1

我有3家公司A,B和C,他们2018年每季度的销售数据分为计算机和打印机类别。

df = pd.DataFrame({"Fiscal Quarter": ["FY18-Q1", "FY18-Q1", "FY18-Q1", "FY18-Q1", "FY18-Q1", "FY18-Q1",
                                      "FY18-Q2", "FY18-Q2", "FY18-Q2", "FY18-Q2", "FY18-Q2", "FY18-Q2",
                                      "FY18-Q3", "FY18-Q3", "FY18-Q3", "FY18-Q3", "FY18-Q3", "FY18-Q3",
                                      "FY18-Q4", "FY18-Q4", "FY18-Q4", "FY18-Q4", "FY18-Q4", "FY18-Q4"],
                   "Company": ["A", "A", "B", "B", "C", "C",
                               "A", "A", "B", "B", "C", "C",
                               "A", "A", "B", "B", "C", "C",
                               "A", "A", "B", "B", "C", "C"],
                   "Category": ["Computers", "Printers", "Computers", "Printers", "Computers", "Printers",
                                "Computers", "Printers", "Computers", "Printers", "Computers", "Printers",
                                "Computers", "Printers", "Computers", "Printers", "Computers", "Printers",
                                "Computers", "Printers", "Computers", "Printers", "Computers", "Printers"],
                   "Sales": [300, 350, 1000, 700, 2500, 2800,
                             450, 200, 1100, 720, 2400, 2100,
                             600, 330, 850, 1200, 2400, 2000,
                             520, 400, 900, 700, 2000, 2200]})

https://github.com/currentlyunknown/sampledata/blob/master/sampledata.csv

我希望“价值”不仅可以作为每家公司的销售额,还可以作为一个季度(A + B + C)的总销售额的百分比,所以作为公司A的一个例子:

               FY18-Q1          FY18-Q2
Computers      300              450
Printers       350              400

期望的输出将是这样的:

               FY18-Q1          FY18-Q2
Computers      300              450
               30%              40%
Printers       350              400
               25%              27%

到目前为止,我使用以下方法为每个公司准备了一个['%of Total']列的df:

total = df.groupby(['Fiscal Quarter', 'Category']).sum().rename(columns={"Sales": "Total Sales"})

df = df.merge(total, on=['Fiscal Quarter', 'Category'])

df['% of Total'] = (df['Sales'] / df['Total Sales'])

df = df.drop(['Total Sales'], axis=1)

我创建数据透视表以分别查看每个公司的销售数据:

dfa = df[df['Company']=='A']

A = pd.pivot_table(
    dfa,
    index=['Category'],
    columns=['Fiscal Quarter'],
    values=['Sales', '% of Total'],
    aggfunc=np.sum
    ).reset_index()

A.columns = A.columns.droplevel([0])
A = A.reset_index().rename_axis(None, axis=1)

但我最终得到:

               FY18-Q1          FY18-Q2          FY18-Q1          FY18-Q2
Computers      300              450              30%              40%
Printers       350              400              25%              27%

现在,我该如何以理想的方式转动它?

python pandas pivot-table
1个回答
0
投票
  1. 计算表副本中的总和(groupby + sum)
  2. 按公司名称合并两个表(您将获得额外的总销售列)
  3. 计算总销售额与确切Q销售额之间的百分比
© www.soinside.com 2019 - 2024. All rights reserved.