1

I have a large dataframe with a lot of columns and want to delete some based on string operations on the column names.

Consider the following example:

df_tmp = pd.DataFrame(data=[(1,2,3, "foo"), ("bar", 4,5,6), (7,"baz", 8,9)],
                     columns=["test", "anothertest", "egg", "spam"]) 

Now, I would like to delete all columns where the column name contains test; I have tried to adapt answers given here (string operations on column content) and here (on addressing the name) to no avail.

df_tmp = df_tmp[~df_tmp.index.str.contains("test")]
# AttributeError: Can only use .str accessor with string values!

df_tmp[~df_tmp.name.str.contains("test")]
# AttributeError: 'DataFrame' object has no attribute 'name'

Can someone point me in the right direction? Thanks a ton in advance. :)

Ivo
  • 3,890
  • 5
  • 22
  • 53
  • 1
    use : `df_tmp.loc[:,~df_tmp.columns.str.contains("test")]` your code didnot work because you were checking the index and not columns, also the correct way to return the `True` columns is via `df.loc[]` – anky Sep 13 '19 at 08:08

1 Answers1

2

Better would be with df.filter()...

>>> df_tmp
  test anothertest  egg spam
0    1           2    3  foo
1  bar           4    5    6
2    7         baz    8    9

Result:

1-

>>> df_tmp.loc[:,~df_tmp.columns.str.contains("test")]
   egg spam
0    3  foo
1    5    6
2    8    9

2-

>>> df_tmp.drop(df_tmp.filter(like='test').columns, axis=1)
   egg spam
0    3  foo
1    5    6
2    8    9

3-

>>> df_tmp.drop(df_tmp.filter(regex='test').columns, axis=1)
   egg spam
0    3  foo
1    5    6
2    8    9

4-

>>> df_tmp.filter(regex='^((?!test).)*$')
   egg spam
0    3  foo
1    5    6
2    8    9

Regex explanation

'^((?!test).)*$'

^         #Start matching from the beginning of the string.    
(?!test)  #This position must not be followed by the string "test".
.         #Matches any character except line breaks (it will include those in single-line mode).
$         #Match all the way until the end of the string.

Nice explanation about regex negative lookahead

Karn Kumar
  • 8,518
  • 3
  • 27
  • 53