代码之家  ›  专栏  ›  技术社区  ›  Chris

熊猫与过滤

  •  -1
  • Chris  · 技术社区  · 5 年前

    我原以为“&”的意思是AND,但在下面的示例中,它的作用类似于OR,因为这应该在index=1处给出最后一行。

    ex=pd.DataFrame([[1,2,"a"],[1,2,"b"]], columns=['e','f','g'])
    ex[(ex.e!=1) & (ex.g!="a")]
    

    enter image description here

    编辑:不适用于“和” enter image description here

    1 回复  |  直到 5 年前
        1
  •  2
  •   BENY    5 年前

    你应该加上 ~

    out = ex[~((ex.e==1) & (ex.g=="a"))]
    print(out)
       e  f  g
    1  1  2  b