如果可能的话,使用切片选择方法之类的方法批量删除数据帧的列?



对于下一个数据帧,我想删除列c, d, e, f, g

    a   b   c   d   e   f   g   h   i   j
0   0   1   2   3   4   5   6   7   8   9
1   10  11  12  13  14  15  16  17  18  19

所以我使用下一个代码:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(20).reshape(2, 10), columns=list('abcdefghij'))
df.drop(['c', 'd', 'e', 'f', 'g'], axis=1)

问题是也许我的数据帧不仅有这么少的列,我可能需要删除很多连续的列,所以我的问题'c': 'g'都可以让我快速选择要删除的列?

使用 DataFrame.loc 选择连续的列名称:

df = df.drop(df.loc[:, 'c':'g'].columns, axis=1)
print (df)
    a   b   h   i   j
0   0   1   7   8   9
1  10  11  17  18  19

或使用Index.isin

c = df.loc[:, 'c':'g'].columns
df = df.loc[:, ~df.columns.isin(c)]

如果可能,多个连续组使用 Index.union 将值连接在一起,Index.isinIndex.differenceIndex.drop

c1 = df.loc[:, 'c':'g'].columns
c2 = df.loc[:, 'i':'j'].columns
df = df.loc[:, ~df.columns.isin(c1.union(c2))]
print (df)
    a   b   h
0   0   1   7
1  10  11  17

df = pd.DataFrame(np.arange(20).reshape(2, 10), columns=list('wbcdefghij'))
print (df)
    w   b   c   d   e   f   g   h   i   j
0   0   1   2   3   4   5   6   7   8   9
1  10  11  12  13  14  15  16  17  18  19
c1 = df.loc[:, 'c':'g'].columns
c2 = df.loc[:, 'i':'j'].columns
#possible change order of columns, because function difference sorting
df1 = df[df.columns.difference(c1.union(c2))]
print (df1)
    b   h   w
0   1   7   0
1  11  17  10
#ordering is not changed
df2 = df[df.columns.drop(c1.union(c2))]
print (df2)
    w   b   h
0   0   1   7
1  10  11  17

最新更新