[英]How to search data in a pivot table in pandas?
I created a pivot table from a dataframe using: 我使用以下方法从数据框创建了一个数据透视表:
table = pd.pivot_table(df , index=['student','year','subject'] , values=['mark'])
and I got a table like this: 我有一张这样的桌子:
student year subject
'Martin' 2014 Algebra 5
Chemistry 3.5
Programming 8
'Sara' 2013 Algebra 2.2
.... .... .....
How can I get Martin 2014 Algebra's mark? 我怎样才能获得Martin 2014 Algebra的标志?
I tried as a dataframe: 我试过作为数据帧:
t[t.student=='Martin'][t.year=2014][t.subject==Algebra]
but it doesn't works 但它不起作用
Can somebody help me? 有人能帮助我吗? Thank's! 谢谢!
You need to reset the index in order to use boolean indexing 您需要重置索引才能使用布尔索引
t= table.reset_index()
t[(t.student=='Martin') & (t.year=2014) & (t.subject=='Algebra')]
Or you can use MultiIndex Selection 或者您可以使用MultiIndex Selection
table.loc[('Martin', 2014, 'Algebra')]
您有一个多索引数据框,使用loc
with tuple来访问该值:
table.loc[("'Martin'",2014,"Algebra")]
the resulting pivot table is just another dataframe. 生成的数据透视表只是另一个数据帧。 However, this dataframe has a multiindex. 但是,此数据框具有多索引。 Multiindexes can be referenced with tuples of the levels. 可以使用级别的元组引用多索引。 This is @psidom's and @TedPetrou's answers 这是@ psidom和@ TedPetrou的答案
However, an alternative would be to use the xs
cross section method 但是,另一种方法是使用xs
横截面方法
table.xs(('Martin', 2014, 'Algebra'))
In this particular case, table is really a Series. 在这种特殊情况下,表格实际上是一个系列。 You can use query
on a dataframe. 您可以在数据框上使用query
。 So we can make it a dataframe and then query
所以我们可以将它作为数据帧然后进行query
table.to_frame().query('student == "Martin" & year == 2014 & subject == "Algebra"')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.