繁体   English   中英

如何在pandas的数据透视表中搜索数据?

[英]How to search data in a pivot table in pandas?

我使用以下方法从数据框创建了一个数据透视表:

table = pd.pivot_table(df , index=['student','year','subject'] , values=['mark'])

我有一张这样的桌子:

student       year        subject 

'Martin'      2014        Algebra      5
                          Chemistry    3.5 
                          Programming  8

'Sara'        2013        Algebra 2.2
 ....         ....        .....

我怎样才能获得Martin 2014 Algebra的标志?

我试过作为数据帧:

 t[t.student=='Martin'][t.year=2014][t.subject==Algebra]

但它不起作用

有人能帮助我吗? 谢谢!

您需要重置索引才能使用布尔索引

t= table.reset_index()
t[(t.student=='Martin') & (t.year=2014) & (t.subject=='Algebra')]

或者您可以使用MultiIndex Selection

table.loc[('Martin', 2014, 'Algebra')]

您有一个多索引数据框,使用loc with tuple来访问该值:

table.loc[("'Martin'",2014,"Algebra")]

生成的数据透视表只是另一个数据帧。 但是,此数据框具有多索引。 可以使用级别的元组引用多索引。 这是@ psidom和@ TedPetrou的答案


但是,另一种方法是使用xs横截面方法

table.xs(('Martin', 2014, 'Algebra'))

在这种特殊情况下,表格实际上是一个系列。 您可以在数据框上使用query 所以我们可以将它作为数据帧然后进行query

table.to_frame().query('student == "Martin" & year == 2014 & subject == "Algebra"')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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