如何删除DataFrame中除某些列以外的所有列? [英] How to delete all columns in DataFrame except certain ones?
本文介绍了如何删除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
我该如何删除除a
和b
之外的每一列?
How would I go about deleting every column besides a
and b
?
这将导致:
a b
1 2
4 3
8 9
我想用一种简单的代码删除这些内容,即删除a
和b
之外的所有列,因为假设我们有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']]
这篇关于如何删除DataFrame中除某些列以外的所有列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文