如何切割以下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太差了。对于任何示例都没有解释。就像文档是由专家为那些已经熟悉熊猫所有功能的人们编写的。
为什么文档没有提供重新生成示例的代码?
答案 0 :(得分:2)
从这开始:
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