Pandas如何对多指标进行条件选择

2024-06-25 23:24:22 发布

您现在位置:Python中文网/ 问答频道 /正文

这是sample data file,我在ipython笔记本中执行了以下操作:

!curl -O http://pbpython.com/extras/sales-funnel.xlsx

df = pd.read_excel('./sales-funnel.xlsx')
df['Status'] = df['Status'].astype('category')
df["Status"].cat.set_categories(["won","pending","presented","declined"],inplace=True)

table = pd.pivot_table(df,
               index=['Manager', 'Status'],
               values=['Price', 'Quantity'],
               columns=['Product'],
               aggfunc={'Price':[np.sum, np.mean], 'Quantity':len},
               fill_value=0
              )

这是table中的数据:

enter image description here

我想选择(Manager=="Debra Henley") & (Status=="won"),它使用query方法:

^{pr2}$

但是如何使用loc执行相同的选择呢?我试过了但没用:

table.loc[['Debra Henley', 'won']]

你们在处理多重索引时通常用什么?最好的办法是什么


更新:目前已找到两个解决方案:

table.xs(('Debra Henley','won'), level=('Manager', 'Status'))
table.loc[[('Debra Henley', 'won')]]

所以我想在用多索引索引索引时应该使用tuples,而不是{}?在


Tags: dfstatusnptablemanagerxlsxpriceloc
3条回答

你的经典答案由@ScottBoston提供。在

除了@jezrael的IndexSlice方法之外,我还将添加这一点作为广度和视角 您也可以使用^{}来获取横截面

table.xs(['Debra Henley', 'won'])

                Product    
Quantity  len   CPU                1
                Maintenance        0
                Monitor            0
                Software           0
Price     mean  CPU            65000
                Maintenance        0
                Monitor            0
                Software           0
          sum   CPU            65000
                Maintenance        0
                Monitor            0
                Software           0
Name: (Debra Henley, won), dtype: int64

可以,您可以使用:

table.loc[[('Debra Henley', 'won')]]

要返回熊猫数据帧,或者可以使用:

^{pr2}$

还一个熊猫系列。在

您可以参考this文档。在

对于更简单的选择(仅索引或仅列),使用^{}方法或按tuples选择。在

另一个更通用的解决方案是slicers

idx = pd.IndexSlice
#output is df
print (table.loc[[idx['Debra Henley','won']]])
                    Quantity                               Price              \
                         len                                mean               
Product                  CPU Maintenance Monitor Software    CPU Maintenance   
Manager      Status                                                            
Debra Henley won           1           0       0        0  65000           0   


                                        sum                               
Product             Monitor Software    CPU Maintenance Monitor Software  
Manager      Status                                                       
Debra Henley won          0        0  65000           0       0        0

^{pr2}$

但是,对于更复杂的选择,如果需要将筛选索引和列放在一起,一个xs不起作用:

idx = pd.IndexSlice
#select all rows where first level is Debra Henley in index and 
#in columns second level is len and sum
print (table.loc[idx['Debra Henley',:], idx[:, ['len', 'sum'], :]])
                       Quantity                               Price  \
                            len                                 sum   
Product                     CPU Maintenance Monitor Software    CPU   
Manager      Status                                                   
Debra Henley won              1           0       0        0  65000   
             pending          1           2       0        0  40000   
             presented        1           0       0        2  30000   
             declined         2           0       0        0  70000   



Product                Maintenance Monitor Software  
Manager      Status                                  
Debra Henley won                 0       0        0  
             pending         10000       0        0  
             presented           0       0    20000  
             declined            0       0        0     

相关问题 更多 >