Python 3.x 删除panda中少于3个非零值的行

Python 3.x 删除panda中少于3个非零值的行,python-3.x,pandas,Python 3.x,Pandas,我想从panda DataFrame中删除小于3个非零值(不包括总计列)的行 所以现在我有 year 2001 2002 2003 2004 2005 2006 2007 TOTAL player Emma 0 0 0 0 3 4 5 12 Max 3 5 0 0 0 0 0 8 Josh 1 2 4 1 2

我想从panda DataFrame中删除小于3个非零值(不包括总计列)的行

所以现在我有

    year    2001 2002 2003 2004 2005 2006 2007 TOTAL
    player  
    Emma    0     0     0    0    3    4    5    12
    Max     3     5     0    0    0    0    0    8
    Josh    1     2     4    1    2    1    0    11
    Steve   0     0     0    0    3    0    0    3
    Mike    1     0     0    0    0    0    2    3
但我想:

    year    2001 2002 2003 2004 2005 2006 2007 TOTAL
    player  
    Emma    0     0     0    0    3    4    5    12
    Josh    1     2     4    1    2    1    0    11
我正在考虑使用for循环,但我不确定如何实现它/它是否是解决我的问题的最佳方法。


I
drop
TOTAl
sum
每行的非零数

numpy

更快的解决方案

设置

df = pd.DataFrame({'2001': {'Emma': 0, 'Josh': 1, 'Max': 3, 'Mike': 1, 'Steve': 0},
 '2002': {'Emma': 0, 'Josh': 2, 'Max': 5, 'Mike': 0, 'Steve': 0},
 '2003': {'Emma': 0, 'Josh': 4, 'Max': 0, 'Mike': 0, 'Steve': 0},
 '2004': {'Emma': 0, 'Josh': 1, 'Max': 0, 'Mike': 0, 'Steve': 0},
 '2005': {'Emma': 3, 'Josh': 2, 'Max': 0, 'Mike': 0, 'Steve': 3},
 '2006': {'Emma': 4, 'Josh': 1, 'Max': 0, 'Mike': 0, 'Steve': 0},
 '2007': {'Emma': 5, 'Josh': 0, 'Max': 0, 'Mike': 2, 'Steve': 0},
 'TOTAL': {'Emma': 12, 'Josh': 11, 'Max': 8, 'Mike': 3, 'Steve': 3}})
解决方案

df.loc[np.sum(df.iloc[:,:-1]>0, axis=1)[lambda x: x>=3].index]
    Out[889]: 
      2001  2002  2003  2004  2005  2006  2007  TOTAL
Emma     0     0     0     0     3     4     5     12
Josh     1     2     4     1     2     1     0     11
或者使用groupby和filter:

df.groupby(level=0).filter(lambda x: np.sum(x.iloc[0,:]>0)>=4)
Out[918]: 
      2001  2002  2003  2004  2005  2006  2007  TOTAL
Emma     0     0     0     0     3     4     5     12
Josh     1     2     4     1     2     1     0     11

谢谢你,伙计!df[df.ne(0).sum(1)>3]在我的浏览器中,粗体/斜体/引号有点狭窄。。。我喜欢粗体/斜体,或者如果需要的话,也喜欢粗体/引号。但是一个用于野外工作的鞋面…@Stephernauch所以我很清楚。。。你是说我用粗体/斜体/引号让你的眼睛流血。如果我想让你继续阅读我的答案,我应该停下来。@stephernauch很高兴知道,我会尝试一种更微妙的格式样式:-)
df.loc[np.sum(df.iloc[:,:-1]>0, axis=1)[lambda x: x>=3].index]
    Out[889]: 
      2001  2002  2003  2004  2005  2006  2007  TOTAL
Emma     0     0     0     0     3     4     5     12
Josh     1     2     4     1     2     1     0     11
df.groupby(level=0).filter(lambda x: np.sum(x.iloc[0,:]>0)>=4)
Out[918]: 
      2001  2002  2003  2004  2005  2006  2007  TOTAL
Emma     0     0     0     0     3     4     5     12
Josh     1     2     4     1     2     1     0     11