Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop column that starts with

I have a data frame that has multiple columns, example:

        Prod_A       Prod_B        Prod_C        State         Region
1          1           0             1             1              1

I would like to drop all columns that starts with Prod_, (I can't select or drop by name because the data frame has 200 variables) Is it possible to do this ?

Thank you

like image 703
hdatas Avatar asked May 06 '17 15:05

hdatas


People also ask

How do I drop a column by name?

First, you define the table name from which you wish to remove or delete the column. Second, you write the column name you want to delete in the DROP clause. A user can also drop multiple columns simultaneously from a table. You can do this by listing out the column names that you want to drop, separated by a comma.

How do I drop multiple columns in ILOC?

You can drop columns by index by using DataFrame. drop() method and by using DataFrame. iloc[]. columns property to get the column names by index.


3 Answers

Use startswith for mask and then delete columns with loc and boolean indexing:

df = df.loc[:, ~df.columns.str.startswith('Prod')]
print (df)
   State  Region
1      1       1
like image 179
jezrael Avatar answered Sep 24 '22 07:09

jezrael


First, select all columns to be deleted:

unwanted = df.columns[df.columns.str.startswith('Prod_')]

The, drop them all:

df.drop(unwanted, axis=1, inplace=True)
like image 32
DYZ Avatar answered Sep 23 '22 07:09

DYZ


we can also use negative RegEx:

In [269]: df.filter(regex=r'^(?!Prod_).*$')
Out[269]:
   State  Region
1      1       1
like image 23
MaxU - stop WAR against UA Avatar answered Sep 26 '22 07:09

MaxU - stop WAR against UA