简体   繁体   English

熊猫中的波浪号(〜)的正式文档在哪里?

[英]Where is official documentation for tilde (~) in Pandas?

I am pretty sure that ~ in Pandas is boolean not . 我敢肯定, ~在大熊猫是布尔not I found a couple of StackOverflow questions / answers, but no pointer to official documentation. 我发现了几个StackOverflow问题/答案,但没有找到官方文档的指针。

Sanity Check 完整性检查

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pandas as pd


df = pd.DataFrame([(1, 2, 1),
                   (1, 2, 2),
                   (1, 2, 3),
                   (4, 1, 612),
                   (4, 1, 612),
                   (4, 1, 1),
                   (3, 2, 1),
                   ],
                  columns=['groupid', 'a', 'b'],
                  index=['India', 'France', 'England', 'Germany', 'UK', 'USA',
                         'Indonesia'])

print(df)
filtered = df[~(df['a'] == 2)]
print(filtered)

The df is df是

           groupid  a    b
India            1  2    1
France           1  2    2
England          1  2    3
Germany          4  1  612
UK               4  1  612
USA              4  1    1
Indonesia        3  2    1

and filtered is filtered

         groupid  a    b
Germany        4  1  612
UK             4  1  612
USA            4  1    1

So I'm pretty sure it is boolean not. 所以我很确定它不是布尔值。

The ~ is the operator equivalent of the __invert__ dunder which has been overridden explicitly for the purpose performing vectorized logical inversions on pd.DataFrame / pd.Series objects. ~是等于__invert__ dunder的运算符,它已被显式覆盖,目的是对pd.DataFrame / pd.Series对象执行矢量化逻辑求逆。

s = pd.Series([True, False])

~s

0    False
1     True
dtype: bool

s.__invert__()

0    False
1     True
dtype: bool

Note: Dunder methods must not be used directly in code, always prefer the use of the operators. 注意:Dunder方法一定不能直接在代码中使用,始终喜欢使用运算符。

Also, since you've asked, the section on Boolean Indexing describes its use. 另外,由于您已提出要求,有关布尔索引的部分介绍了其用法。

Another common operation is the use of boolean vectors to filter the data. 另一个常见的操作是使用布尔向量来过滤数据。 The operators are: | 运营商为: | for or , & for and , and ~ for not . or&and ,和~ not These must be grouped by using parentheses. 这些必须使用括号进行分组。

Bold emphasis mine. 大胆强调我的。

I found it referenced on this page. 我在页面上找到了它。 It's about halfway down, I'd navigate to it with ctrl+F. 它大约是一半,我可以使用ctrl + F导航到它。 You're correct though, it's the not operator. 没错,这not操作符。

Here they define explicitly: 他们在这里明确定义:

Another common operation is the use of boolean vectors to filter the data. 另一个常见的操作是使用布尔向量来过滤数据。 The operators are: | 操作员是: for or, & for and, and ~ for not . 为or,&为and,和〜为not These must be grouped by using parentheses. 这些必须使用括号进行分组。

如果您转到: https : //docs.python.org/3/library/operator.html ,它说:

Bitwise Inversion   ~ a invert(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM