我对Python和这个社区相当陌生,所以请原谅我的业余尝试来解释我对最可能非常明显的事情的深刻理解。无论如何..在通过.xs进行切片时使用multiIndex DataFrame上的any()和all():奇怪的行为还是仅仅是我?
我有一个数据框称为“数据”。它由多个索引组成,包括“日期”和“屁”。
有一列叫做“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
我认为这将返回无论是否在任何地点存在我的数据框片内的值,因此结果是真实的吗?
我:
data.xs(farts[1], level = 1).any()
计算机:
integrated_daily_difference True
dtype: bool
OK,这都挺有道理。现在的怪异的东西..
我:
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。我似乎没有正确使用任何(),所以我正在寻求帮助。
任何输入表示赞赏。先谢谢你!
这非常有帮助。谢谢!我实际上做了更深入的尝试,并尝试了以下内容:mylist = [1,2,3,4,0,-1] print'test 1:',all(mylist)> -1 print'test 2:',所有(对于mylist中的i> -1)测试1打印为True并且测试2打印为False。我假设这是由于True(1)和False(0)对于第一种情况都大于-1,并且当在发生器中进行评估时它的行为有所不同。 –
让我们先尝试一下,'all(mylist) '再次没有条件,但是当列表包含0时,它会返回False,它的数值为0,那么为什么当用“> -1”测试时它会返回True。 – hashcode55
,在第二个中,您正在测试一组布尔值! i> -1将返回False为-1,这将导致整体False,尝试> = -1,您将变为True!它只是你必须牢记的基本概念.....“如果迭代的任何元素都是真的,则返回True。” - 对于任何和“返回True,如果迭代的所有元素都是真的” - 对于所有 – hashcode55