gpt4 book ai didi

python - 如何从 df.columns 中删除名称?

转载 作者:太空宇宙 更新时间:2023-11-04 09:37:28 25 4
gpt4 key购买 nike

我想知道如何从 df.columns 属性中删除 name:

假设我有以下带有 df.columns 的数据框:

df = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6]})
df

a b
0 1 4
1 2 5
2 3 6

df.columns

Index(['a', 'b'], dtype='object')

然后,如果我向列属性添加名称,那么我将获得数据框:

df.columns.name = 'test'
df.columns

Index(['a', 'b'], dtype='object', name='test')

df

test a b
0 1 4
1 2 5
2 3 6

有没有办法删除出现在索引上方的名称 test

最佳答案

可以通过del删除属性:

df.columns.name = 'test'
print(df.columns)
# Index(['a', 'b'], dtype='object', name='test')

del df.columns.name
print(df.columns)
# Index(['a', 'b'], dtype='object')

关于python - 如何从 df.columns 中删除名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53123093/

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