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
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.
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.
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
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)
we can also use negative RegEx:
In [269]: df.filter(regex=r'^(?!Prod_).*$')
Out[269]:
State Region
1 1 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With