Dinosaurius
Dinosaurius

Reputation: 8628

How to delete columns based on condition

I want to delete columns that start the particular "TYPE" word and do not contain _1?

df =

TYPE_1    TYPE_2    TYPE_3    COL1
aaa       asb       bbb       123

The result should be:

df =

    TYPE_1    COL1
    aaa       123

Currently I am deleting these columns manually, however this approach is not very efficient if the number of columns is big:

df = df.drop(["TYPE_2","TYPE_3"], axis=1)

Upvotes: 4

Views: 12477

Answers (4)

Ted Petrou
Ted Petrou

Reputation: 61947

This is the fifth answer but I wanted to showcase the power of the filter dataframe method which filters by column names with regex. This searches for columns that don't start with TYPE or have _1 somewhere in them.

df.filter(regex='^(?!TYPE)|_1')

Upvotes: 6

nipy
nipy

Reputation: 5488

A list comprehension can be used. Note: axis=1 denotes that we are referring to the column and inplace=True can also be used as per pandas.DataFrame.drop docs.

droplist = [i for i in df.columns if i.startswith('TYPE') and '_1' not in i]
df1.drop(droplist,axis=1,inplace=True)

Upvotes: 8

Steve Barnes
Steve Barnes

Reputation: 28370

t_cols = [c for c in df.columns.values if c.startswith('TYPE_') and not c == 'TYPE_1']
df.drop(t_cols)

Should do the job

Upvotes: 1

DYZ
DYZ

Reputation: 57033

Easy:

unwanted = [column for column in df.columns 
            if column.startswith("TYPE") and "_1" not in column]
df = df.drop(unwanted)

Upvotes: 2

Related Questions