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

马丁·托马

我很确定~在Pandas中是boolean not我发现了几个StackOverflow问题/答案,但没有找到官方文档的指针。

完整性检查

#!/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)

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

并且filtered

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

所以我很确定它不是布尔值。

cs95

~是操作者的等效的__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

注意:Dunder方法一定不能直接在代码中使用,始终喜欢使用运算符。

另外,由于您已提出要求,有关布尔索引的部分介绍了其用法。

另一个常见的操作是使用布尔向量来过滤数据。运算符为:|for or&forand~fornot这些必须通过使用括号进行分组。

大胆强调我的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章