如何删除DataFrame中除某些列以外的所有列?

问题描述:

假设我有一个看起来像这样的DataFrame:

Let's say I have a DataFrame that looks like this:

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

我该如何删除除ab之外的每一列?

How would I go about deleting every column besides a and b?

这将导致:

a  b
1  2
4  3
8  9

我想用一种简单的代码删除这些内容,即删除ab之外的所有列,因为假设我们有1000列数据.

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.

谢谢.

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

或:

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请注意, 最惯用的熊猫方式已由@Wen提出:

PS please be aware that the most idiomatic Pandas way to do that was already proposed by @Wen:

df = df[['a','b']]

df = df.loc[:, ['a','b']]