gpt4 book ai didi

python - 向多索引数据帧添加级别

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:00 24 4
gpt4 key购买 nike

我有一个多索引数据框:

iterables = [['bar', 'baz', 'foo', 'qux'], ['one', 'two']]
Index = pd.MultiIndex.from_product(iterables, names=['first', 'second'])
s = pd.DataFrame(np.random.randn(8,2), index=Index)

它产生如下数据帧:

                     0         1
first second
bar one 0.619602 -0.422137
two -0.372906 0.581697
baz one -0.968921 -0.014957
two -0.470649 -1.706410
foo one 0.834609 -0.600675
two 0.005306 0.109989
qux one -0.713642 -0.173100
two -1.155766 -0.365946

现在,在 multiIndex 的“第二”级别下,我想添加“三”,并使其等于“一”和“二”之间的差

在下面,A1 将等于 1.674156-(-1.061293)

A2 将等于 -1.380391-(-0.620890)

像这样:

                     0         1
first second
bar one 1.674156 -1.380391
two -1.061293 -0.620890
three A1 A2
baz one 0.839065 -1.985679
two -2.086971 -1.415384
three
foo one -1.673192 -0.559783
two 0.135445 -1.101833
three
qux one -0.605042 1.814256
two 0.182851 -1.819808
three

我该怎么做?我知道我可以取消 level1 的堆栈,进行比较,然后堆栈回来。只是好奇有更好的解决方案吗?

最佳答案

使用堆栈/取消堆栈/分配

d = s.stack().unstack(1)
d.assign(three=d.one - d.two).stack().unstack(1)

0 1
first second
bar one 0.877453 0.214777
two -0.741437 0.185339
three 1.618890 0.029437
baz one 1.358314 -0.315129
two 1.881061 0.253034
three -0.522747 -0.568162
foo one 1.663033 -0.879386
two -0.658539 -0.331162
three 2.321572 -0.548224
qux one -0.171216 -0.510144
two 0.855199 -0.653881
three -1.026415 0.143737

关于python - 向多索引数据帧添加级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42498761/

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