简体   繁体   English

关键错误数据透视表熊猫

[英]Key Error pivot table pandas

Trying to drop certain row from pivot table. 尝试从数据透视表中删除某些行。 However, whenever I try to drop the row I get a key error. 但是,每当我尝试删除该行时,都会得到一个关键错误。 I've looked into reshaping pivot tables and multi-indexing, but I'm having trouble grasping the concepts. 我研究过重塑数据透视表和多索引的问题,但是在理解这些概念时遇到了麻烦。 Are they even necessary for this situation? 在这种情况下它们是否甚至必要? If so, can you please explain? 如果可以,请您解释一下? If not, what's the most efficient way to fix the issue? 如果不是,最有效的解决方法是什么?

ps_total = ps.pivot_table(index="CountyName",columns="Year",values=["Number of Private Schools"],aggfunc=np.sum)
ps_total.columns = pd.MultiIndex.from_tuples(ps_total.columns) 
ps_total = ps_total[(ps_total["CountyName"]==1).any(axis=1)] # error here
ps_total = ps_total.fillna(0)
print(ps_total.to_string())



                                     2010   2011   2012   2013   2014   2015  
CountyName                                                                         
1                                     0.0    0.0    0.0    0.0    0.0    3.0    
Alameda                             156.0  144.0  148.0  145.0  141.0  136.0  
Alpine                                0.0    0.0    1.0    1.0    0.0    0.0    
Amador                                2.0    2.0    1.0    1.0    1.0    0.0    
Butte                                21.0   21.0   22.0   21.0   19.0   16.0  

 Key Error: "CountyName"

ps_total is a MultiIndex named "CountyName". ps_total是一个MultiIndex命名为“CountyName”。 It does not have the key "CountyName". 它没有键“ CountyName”。 You are probably looking for ps_total==1 . 您可能正在寻找ps_total==1

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

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