gpt4 book ai didi

python - 从 MultiIndex Pandas 数据框中删除一列

转载 作者:太空宇宙 更新时间:2023-11-03 14:46:17 25 4
gpt4 key购买 nike

我一直在努力理解 Pandas 的多索引方法。我试图删除“std”子列,但徒劳无功。

如何做到这一点?

                                    attribute                           attribute2  \
test1 std test2
d count type
r1 10 rx 0.559 (0.0) 0.559 (0.0) 0.568 (0.0)
sth1 0.653 (0.004) 0.653 (0.004) 0.679 (0.002)
sth2 0.584 (0.002) 0.584 (0.002) 0.586 (0.003)
sth3 0.651 (0.005) 0.651 (0.005) 0.676 (0

我似乎无法理解 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.MultiIndex.drop.html函数,我相信。

因此,生成的数据框应该只有两个平均列,没有“std”非常感谢。

最佳答案

我认为更好的方法是删除所有带有 std 的列是 MultiIndex 使用 drop带参数 level=1:

print (df)
attribute attribute2 attribute3
test1 std test2 std test3 std
d count type
r1 10 rx 0.559 (0.0) 0.559 (0.0) 0.568 (0.0)
sth1 0.653 (0.004) 0.653 (0.004) 0.679 (0.002)
sth2 0.584 (0.002) 0.584 (0.002) 0.586 (0.003)
sth3 0.651 (0.005) 0.651 (0.005) 0.676 (0)

df = df.drop('std', axis=1, level=1)
print (df)
attribute attribute2 attribute3
test1 test2 test3
d count type
r1 10 rx 0.559 0.559 0.568
sth1 0.653 0.653 0.679
sth2 0.584 0.584 0.586
sth3 0.651 0.651 0.676

关于python - 从 MultiIndex Pandas 数据框中删除一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49034807/

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