Python 如何删除DataFrame中除某些列之外的所有列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45846189/
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
How to delete all columns in DataFrame except certain ones?
提问by sgerbhctim
Let's say I have a DataFrame that looks like this:
假设我有一个如下所示的 DataFrame:
a b c d e f g
1 2 3 4 5 6 7
4 3 7 1 6 9 4
8 9 0 2 4 2 1
How would I go about deleting every column besides a
and b
?
我将如何删除除a
and之外的每一列b
?
This would result in:
这将导致:
a b
1 2
4 3
8 9
I would like a way to delete these using a simple line of code that says, delete all columns besides a
and b
, because let's say hypothetically I have 1000 columns of data.
我想要一种使用简单的代码行删除这些内容的方法,删除除a
and之外的所有列b
,因为假设我有 1000 列数据。
Thank you.
谢谢你。
回答by MaxU
In [48]: df.drop(df.columns.difference(['a','b']), 1, inplace=True)
Out[48]:
a b
0 1 2
1 4 3
2 8 9
or:
或者:
In [55]: df = df.loc[:, df.columns.intersection(['a','b'])]
In [56]: df
Out[56]:
a b
0 1 2
1 4 3
2 8 9
PS please be aware that the most idiomatic Pandas wayto do that was already proposed by @Wen:
PS 请注意,@Wen 已经提出了最惯用的 Pandas 方法:
df = df[['a','b']]
or
或者
df = df.loc[:, ['a','b']]
回答by GollyJer
Another option to add to the mix. I prefer this approach for readability.
添加到组合中的另一种选择。我更喜欢这种方法的可读性。
df = df.filter(['a', 'b'])
or simply
或者干脆
df.filter(['a', 'b'])
Bonus
奖金
You can also use a like
argument or regex
to filter.
Helpful if you have a set of columns like ['a_1','a_2','b_1','b_2']
您还可以使用like
参数或regex
进行过滤。
如果您有一组像['a_1','a_2','b_1','b_2']
You could do
你可以做
df.filter(like='b_')
and end up with ['b_1','b_2']
并以 ['b_1','b_2']
回答by YOBEN_S
there are multiple solution .
有多种解决方案。
df=df[['a','b']]#1
df=df[list('ab')]#2
df=df.loc[:,df.columns.isin(['a','b'])]#3
df=pd.DataFrame(data=df.eval('a,b').T,columns=['a','b'])#4 PS:I do not recommend this method , but still a way to achieve this
回答by Taie
If you have more than two columns that you want to drop, let's say 20
or 30
, you can use lists as well. Make sure that you also specify the axis value.
如果您要删除的列多于两列,例如20
或30
,您也可以使用列表。确保您还指定了轴值。
drop_list = ["a","b"]
df = df.drop(df.columns.difference(drop_list), axis=1)
回答by Isaac Taylor
If you only want to keep more columns than you're dropping put a "~" before the .isin statement to select every column except the ones you want:
如果您只想保留多于您删除的列,请在 .isin 语句前添加“~”以选择除您想要的列之外的每一列:
df = df.loc[:, ~df.columns.isin(['a','b'])]