如何 select 来自 Pandas 的数据透视 table 并用 0 填充缺失值?

How to select data from Pandas pivot table and fill missing values with 0?

我需要一些关于 pandas 枢轴 table 的指导:

我的代码:

def update_graph(Manager):
    if Manager == "All Managers":
        df_plot = df.copy()
    else:
       df_plot = df[df['Manager'] == Manager]

    pv = pd.pivot_table(
        df_plot,
        index=['Name'],
        columns=["Status"],
        values=['Quantity'],
        aggfunc=sum,
        fill_value=0)

myData.csv:

Account,Name,Manager,Quantity,Status
123,APAC,John,10,closed
1234,EMEA,Mike,4,open
12345,LATAM,Boris,2,escalated
123456,NAM,Jack,1,pending
123456,NAM,Mike,2,escalated
12345,LATAM,Sam,2,open

为 'All Managers' 返回的数据:

       Quantity                       
Status   closed escalated open pending
Name                                  
APAC         10         0    0       0
EMEA          0         0    4       0
LATAM         0         2    2       0
NAM           0         2    0       1

如果 'Mike' 选择为 'Manager' 则返回数据:

        Quantity     
Status escalated open
Name                 
EMEA           0    4
NAM            2    0

如果我不为 Mike 的案例提供 'pending' 和 'closed' 值,数据将不会显示在我的图表上。有人可以帮我修改 pd.pivot_table 以便它捕获 Quantity 为 0 对于所有丢失的 Status(es)?

预计:

       Quantity                       
Status   closed escalated open pending
Name                                  
APAC          0         0    0       0
EMEA          0         0    4       0
LATAM         0         0    0       0
NAM           0         2    0       0

重新索引函数:

def update_graph(Manager):
    if Manager == "All Managers":
        df_plot = df.copy()
    else:
        df_plot = df[df['Manager'] == Manager]

    pv = pd.pivot_table(
        df_plot,
        index='Name',
        columns='Status',
        values='Quantity',
        aggfunc=sum,
        fill_value=0)
    pv = pv.reindex(index=df['Name'].unique(), 
                     columns=df['Quantity'].unique(), 
                     fill_value=0)

重建索引后的结果:

    Status  10  4   2   1 
Name                  
APAC     0   0   0   0
EMEA     0   0   0   0
LATAM    0   0   0   0
NAM      0   0   0   0

首先通过原始 DataFrame 的 NameStatus 列的唯一值从 DataFrame.pivot_table for one element lists for avoid MultiIndex in columns and then use DataFrame.reindex 中删除 []

def update_graph(Manager):
    if Manager == "All Managers":
        df_plot = df.copy()
    else:
       df_plot = df[df['Manager'] == Manager]


    pv = pd.pivot_table(
        df_plot,
        index='Name',
        columns="Status",
        values='Quantity',
        aggfunc=sum,
        fill_value=0)

    return pv.reindex(index=df['Name'].unique(), 
                       columns=df['Status'].unique(), 
                       fill_value=0)

print (update_graph('All Managers'))
Status  closed  open  escalated  pending
Name                                    
APAC        10     0          0        0
EMEA         0     4          0        0
LATAM        0     2          2        0
NAM          0     0          2        1

print (update_graph('John'))
Status  closed  open  escalated  pending
Name                                    
APAC        10     0          0        0
EMEA         0     0          0        0
LATAM        0     0          0        0
NAM          0     0          0        0