在pandas数据帧中,我有一系列布尔值。为了筛选布尔值为真的行,我可以使用:df[df.column_x]
我想为了只过滤列为False的行,我可以使用:df[~df.column_x]。我觉得我以前做过这件事,并把它看作是公认的答案。在
但是,这失败了,因为~df.column_x将值转换为整数。见下文。在import pandas as pd . # version 0.24.2
a = pd.Series([‘a’, ‘a’, ‘a’, ‘a’, ‘b’, ‘a’, ‘b’, ‘b’, ‘b’, ‘b’])
b = pd.Series([True, True, True, True, True, False, False, False, False, False], dtype=bool)
c = pd.DataFrame(data=[a, b]).T
c.columns = [‘Classification’, ‘Boolean’]`
print(~c.Boolean)
0 -2
1 -2
2 -2
3 -2
4 -2
5 -1
6 -1
7 -1
8 -1
9 -1
Name: Boolean, dtype: object
print(~b)
0 False
1 False
2 False
3 False
4 False
5 True
6 True
7 True
8 True
9 True
dtype: bool
基本上,我可以使用c[~b],但不能使用{}
我是不是在梦想着这件事能起作用?在
Original: https://blog.csdn.net/weixin_28756347/article/details/113500401
Author: 音尘tol
Title: python中boolean是什么意思_python的~在使用boolean时发生了什么?
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/742397/
转载文章受原作者版权保护。转载请注明原作者出处!