pandas 删除除一列之外的所有熊猫数据框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19062612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:12:08  来源:igfitidea点击:

Delete all but one column of pandas dataframe?

pythonpandas

提问by goldisfine

I have a pandas dataframe and would like to drop all columns save the index and a column named 'bob'

我有一个Pandas数据框,想删除所有列,保存索引和一个名为“bob”的列

How would I do this?

我该怎么做?

回答by Andy Hayden

You can simply write:

你可以简单地写:

df = df[['bob']]

and the other columns will be garbage collected.

其他列将被垃圾收集。

回答by Alan A. Gulle

you can use df.loc accessor

您可以使用 df.loc 访问器

df=df.loc[:,['bob']]