gpt4 book ai didi

python - 如何按值删除列?

转载 作者:行者123 更新时间:2023-12-01 09:07:40 25 4
gpt4 key购买 nike

我有 df ,其中两列的名称相同。

df:

type     type
1 default
2 default

如何删除 value = default 的整个列?

新的 df 看起来像这样:

type
1
2

最佳答案

使用loc并选择它们:

df = df.loc[:, ~df.eq('default').any(0)]  # ~df.eq('default').all(0)  # Stricter.
print (df)
type
0 1
1 2

您不能使用 dropfilter 或任何其他惯用的列删除技术,因为列名称相同。您唯一的选择是基于 bool 选择器的方法。

关于python - 如何按值删除列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51914177/

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