在通过.xs切片时使用multiIndex DataFrame上的any()和all():奇怪的行为还是仅仅是我?

时间:2016-07-09 10:10:20

标签: python pandas dataframe multi-index any

我对Python和这个社区相当陌生,所以请原谅我的业余尝试解释我对很可能非常明显的事情的深刻困惑。反正..

我有一个名为" data"的数据框。它是multiIndexed,包含2个级别:" date"和"放屁"。

有一个名为:" integrated_daily_difference"。

你可以假设"放屁"是类型:' pandas.core.index.Index'并通过以下方式创建: farts = data.index.levels[1]

现在让我们想象一下,我想在屁的任意索引值处获取我的数据帧的切片视图:即放屁[1]

我:

data.xs(farts[1], level = 1)

计算机:

                  integrated_daily_difference
        date    
2015-05-21 00:00:00+00:00    0.000000
2015-05-22 00:00:00+00:00    0.000000
2015-05-26 00:00:00+00:00   -0.024497
2015-05-27 00:00:00+00:00   -0.051144
2015-05-28 00:00:00+00:00   -0.079841
2015-05-29 00:00:00+00:00   -0.106666
2015-06-01 00:00:00+00:00   -0.131245
2015-06-02 00:00:00+00:00   -0.157428
2015-06-03 00:00:00+00:00   -0.184057
2015-06-04 00:00:00+00:00   -0.209755
2015-06-05 00:00:00+00:00   -0.234588
2015-06-08 00:00:00+00:00   -0.262365
2015-06-09 00:00:00+00:00   -0.291890
2015-06-10 00:00:00+00:00   -0.320943
2015-06-11 00:00:00+00:00   -0.352627
2015-06-12 00:00:00+00:00   -0.381425
2015-06-15 00:00:00+00:00   -0.404055

我:

data.xs(farts[1], level = 1) < 0 

计算机:

                integrated_daily_difference
         date   
2015-05-21 00:00:00+00:00   False
2015-05-22 00:00:00+00:00   False
2015-05-26 00:00:00+00:00   True
2015-05-27 00:00:00+00:00   True
2015-05-28 00:00:00+00:00   True
2015-05-29 00:00:00+00:00   True
2015-06-01 00:00:00+00:00   True
2015-06-02 00:00:00+00:00   True
2015-06-03 00:00:00+00:00   True
2015-06-04 00:00:00+00:00   True
2015-06-05 00:00:00+00:00   True
2015-06-08 00:00:00+00:00   True
2015-06-09 00:00:00+00:00   True
2015-06-10 00:00:00+00:00   True
2015-06-11 00:00:00+00:00   True
2015-06-12 00:00:00+00:00   True
2015-06-15 00:00:00+00:00   True

我假设这会返回切片数据框中任何位置是否存在值,因此结果为True?

我:

data.xs(farts[1], level = 1).any()

计算机:

integrated_daily_difference    True
dtype: bool
好的,这一切都有道理。现在是奇怪的东西..

我:

data.xs(farts[1], level = 1).any() < 0

计算机:

integrated_daily_difference    False
dtype: bool

咦....吗

我:

data.xs(farts[1], level = 1).any(axis = 0) < 0

计算机:

integrated_daily_difference    False
dtype: bool

我:

data.xs(farts[1], level = 1).any(axis = 1) < 0

计算机:

       date
2015-05-21 00:00:00+00:00    False
2015-05-22 00:00:00+00:00    False
2015-05-26 00:00:00+00:00    False
2015-05-27 00:00:00+00:00    False
2015-05-28 00:00:00+00:00    False
2015-05-29 00:00:00+00:00    False
2015-06-01 00:00:00+00:00    False
2015-06-02 00:00:00+00:00    False
2015-06-03 00:00:00+00:00    False
2015-06-04 00:00:00+00:00    False
2015-06-05 00:00:00+00:00    False
2015-06-08 00:00:00+00:00    False
2015-06-09 00:00:00+00:00    False
2015-06-10 00:00:00+00:00    False
2015-06-11 00:00:00+00:00    False
2015-06-12 00:00:00+00:00    False
2015-06-15 00:00:00+00:00    False

我:

data.xs(farts[1], level = 1).any(axis = 1) <= 0

计算机:

        date
2015-05-21 00:00:00+00:00     True
2015-05-22 00:00:00+00:00     True
2015-05-26 00:00:00+00:00    False
2015-05-27 00:00:00+00:00    False
2015-05-28 00:00:00+00:00    False
2015-05-29 00:00:00+00:00    False
2015-06-01 00:00:00+00:00    False
2015-06-02 00:00:00+00:00    False
2015-06-03 00:00:00+00:00    False
2015-06-04 00:00:00+00:00    False
2015-06-05 00:00:00+00:00    False
2015-06-08 00:00:00+00:00    False
2015-06-09 00:00:00+00:00    False
2015-06-10 00:00:00+00:00    False
2015-06-11 00:00:00+00:00    False
2015-06-12 00:00:00+00:00    False
2015-06-15 00:00:00+00:00    False

我:

data.xs(farts[1], level = 1).any(axis = 0) <= 0

计算机:

integrated_daily_difference    False
dtype: bool

然后我的电脑开始疯狂地对我笑,我的脑袋爆炸......

但更严重的是,这里发生了什么?我的目标是尝试检查单列数据框中的所有值或任何值是否满足条件并返回布尔值True或False。我似乎没有正确使用任何(),所以我寻求帮助。

任何输入都表示赞赏。提前谢谢!

2 个答案:

答案 0 :(得分:2)

考虑这个简单的系列:

import numpy as np
np.random.seed(0)
ser = pd.Series(np.random.randint(0, 3, 10))

ser
Out[78]: 
0    0
1    1
2    0
3    1
4    1
5    2
6    0
7    2
8    0
9    0
dtype: int32

假设您要进行比较ser < 2,它将返回一个布尔数组:

ser < 2
Out[79]: 
0     True
1     True
2     True
3     True
4     True
5    False
6     True
7    False
8     True
9     True
dtype: bool

现在,如果要检查其中是否有任何一个小于2,则需要在此阵列上调用any

(ser < 2).any()
Out[81]: True

如果True数组中的至少一个值为ser < 2,则会返回True.all()类似:

(ser < 2).all()
Out[82]: False

由于并非所有这些都是True,因此返回False。如果您将其更改为:

(ser < 3).all()
Out[83]: True

因为它会检查(ser < 3)数组,并且该数组中的所有元素都是True

现在让我们试试ser.any()

ser.any()
Out[84]: True

在这里,您要检查原始数组中的任何值是否为True(如果0为True,如果1为True等)。此数组中的值是整数,而不是布尔值。如果它们不等于0,它们将被评估为True。因此,由于该数组中至少有一个非零,它将返回True

现在,如果我检查ser.any() < 0,它将返回False

ser.any() < 0
Out[85]: False

这是因为此表达式的计算结果为True < 0

True < 0
Out[86]: False

它是False,因为True不小于0.您正在做的是类似的:

data.xs(farts[1], level = 1).any() < 0

它首先在该部分执行any(),并返回True,因为该部分具有非零元素。如果您确实想要检查它们中的任何一个是否小于0,则应键入:

(data.xs(farts[1], level = 1) < 0).any()

(data.xs(farts[1], level = 1) < 0)将创建一个布尔数组,如果该数组中的任何元素为True.any()也将返回True

答案 1 :(得分:1)

首先让我根据pandas-

的文档定义任何方法
  

返回任何元素在请求的轴上是否为True

现在当你写 -

data.xs(farts[1], level = 1).any()

它只是检查是否有任何值是真实的或不是而是,因为没有任何条件,只需检查数字,这意味着0将被视为False和任何其他数字为True。现在数字,其他为0,它返回True。

现在你检查 -

data.xs(farts[1], level = 1).any() < 0 

但是当表示为整数时,True为1,False为0,因此返回False,因为data.xs(farts[1], level = 1).any()的输出为True,即为1.因此,如果您要检查

data.xs(farts[1], level = 1).any() == 1

它将返回True。

现在让我们看看你做了什么 -

data.xs(farts[1], level = 1).any(axis = 1) <= 0 

首先你改变了轴,现在data.xs(farts[1], level = 1).any(axis = 1)根据值返回Trues和Falses(对于0以外的值为True / 1,对于0为0的值为False / 0)。现在因为前两个值是0s / False 并且它满足条件&#34;&lt; = 0&#34;它为您提供了所看到的输出。尝试做 -

data.xs(farts[1], level = 1).any(axis = 1) == 1

你将获得正好相反的输出。

与any()相反,all()的工作方式不同...... 如果all为True或全部为False,则返回true,否则返回False。

还有 -

如果你想的话,

anyoralland不一样...... 或者是和按位操作,它们遵循short circuit evaluation,但是任何和所有功能都会在所有条件下完成。

希望有所帮助:)