gpt4 book ai didi

python - 多索引/高级索引,其中级别不是 (!=) 值

转载 作者:行者123 更新时间:2023-12-01 03:48:31 25 4
gpt4 key购买 nike

如何对以下 df 进行切片,使得第二级 != 2。

在我的现实情况中,我的第二级是日期范围,我希望能够选择除一个日期之外的所有内容。

来自MultiIndex / Advanced Indexing

In [1]: arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
In [2]: tuples = list(zip(*arrays))
In [4]: index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
In [16]: df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)
In [38]: df = df.T

In [65]: df
Out[65]:
A B C
first second
bar one 0.895717 0.410835 -1.413681
two 0.805244 0.813850 1.607920
baz one -1.206412 0.132003 1.024180
two 2.565646 -0.827317 0.569605
foo one 1.431256 -0.076467 0.875906
two 1.340309 -1.187678 -2.211372
qux one -1.170299 1.130127 0.974466
two -0.226169 -1.436737 -2.006747

In [66]: df.xs('one', level='second')
Out[66]:
A B C
first
bar 0.895717 0.410835 -1.413681
baz -1.206412 0.132003 1.024180
foo 1.431256 -0.076467 0.875906
qux -1.170299 1.130127 0.974466

我很惊讶 @pandas.pydata.org 的文档如此糟糕。任何示例都没有解释。就好像该文档是由专家为那些已经熟悉 pandas 所有功能的人编写的。

为什么文档没有提供重新生成示例的代码?

最佳答案

从这里开始:

                    A         B         C
first second
bar one -0.350640 -1.761671 0.253923
two -0.036557 0.212322 0.537106
baz one -1.597584 -0.301356 -0.634428
two 2.340900 -0.356272 -0.985386
foo one 0.122753 -0.333827 -0.620175
two 0.423211 -0.570563 -1.245026
qux one -0.972814 -0.878836 -1.030892
two 0.312855 -0.191677 0.700006


df.iloc[df.index.get_level_values('second') != 'one' ]

A B C
first second
bar two -0.036557 0.212322 0.537106
baz two 2.340900 -0.356272 -0.985386
foo two 0.423211 -0.570563 -1.245026
qux two 0.312855 -0.191677 0.700006


df.iloc[df.index.get_level_values('second') != 'two' ]
A B C
first second
bar one -0.350640 -1.761671 0.253923
baz one -1.597584 -0.301356 -0.634428
foo one 0.122753 -0.333827 -0.620175
qux one -0.972814 -0.878836 -1.030892

关于python - 多索引/高级索引,其中级别不是 (!=) 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38557709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com