代码之家  ›  专栏  ›  技术社区  ›  Nirojan Selvanathan

pandas数据帧中的颚化符签名

  •  56
  • Nirojan Selvanathan  · 技术社区  · 7 年前

    df = df[~df['InvoiceNo'].str.contains('C')]
    

    如果我能知道波浪符号在这种情况下的用法,我将不胜感激?

    4 回复  |  直到 3 年前
        1
  •  56
  •   jezrael    7 年前

    它表示按位非,反转布尔掩码- False s至 True 真的 s至 s

    示例:

    df = pd.DataFrame({'InvoiceNo': ['aaC','ff','lC'],
                       'a':[1,2,5]})
    print (df)
      InvoiceNo  a
    0       aaC  1
    1        ff  2
    2        lC  5
    
    #check if column contains C
    print (df['InvoiceNo'].str.contains('C'))
    0     True
    1    False
    2     True
    Name: InvoiceNo, dtype: bool
    
    #inversing mask
    print (~df['InvoiceNo'].str.contains('C'))
    0    False
    1     True
    2    False
    Name: InvoiceNo, dtype: bool
    

    boolean indexing

    df = df[~df['InvoiceNo'].str.contains('C')]
    print (df)
      InvoiceNo  a
    1        ff  2
    

    因此,输出是数据帧的所有行,其中不包含 C 编队 InvoiceNo .

        2
  •  2
  •   RobinFrcd    7 年前
        3
  •  0
  •   Pasindu Perera    3 年前
    df = df[~df['InvoiceNo'].str.contains('C')]
    

    上面的代码块表示从pandas数据帧中删除所有数据元组,该数据帧在[InvoiceNo]列的字符串值中有“C”字母。

    波浪号(~)符号用作NOT(!)此场景中的操作员。

    通常上述语句用于从数据列中删除具有空值的数据元组 .

        4
  •  -1
  •   Haz    3 年前