pandas 波浪号在python数据框中签名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46054318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:23:48  来源:igfitidea点击:

Tilde sign in python dataframe

pythonpandas

提问by Nirojan Selvanathan

Im new to python and came across a code snippet.

我是 Python 新手,遇到了一个代码片段。

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

Would be much obliged if I could know whats the tilde signs usage in this context ?

如果我能知道在这种情况下波浪号的用法是什么,我会非常感激吗?

采纳答案by jezrael

It means bitwise not, inversing boolean mask - Falses to Trues and Trues to Falses.

这意味着按位不,反转布尔掩码 - Falses 到Trues 和 Trues 到Falses。

Sample:

样本:

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

Filter by boolean indexing:

过滤条件boolean indexing

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

So output is all rows of DataFrame, which not contains Cin column InvoiceNo.

所以输出是DataFrame的所有行,C列中不包含InvoiceNo

回答by RobinFrcd

It's used to invert boolean Series, see pandas-doc.

它用于反转布尔系列,请参阅pandas-doc