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
Tilde sign in python dataframe
提问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 - False
s to True
s and True
s to False
s.
这意味着按位不,反转布尔掩码 - False
s 到True
s 和 True
s 到False
s。
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 C
in column InvoiceNo
.
所以输出是DataFrame的所有行,C
列中不包含InvoiceNo
。
回答by RobinFrcd
It's used to invert boolean Series, see pandas-doc.
它用于反转布尔系列,请参阅pandas-doc。