Column that starts with

I have a data frame with multiple columns, for example:

        Prod_A       Prod_B        Prod_C        State         Region
1          1           0             1             1              1

I would like to delete all columns starting with Prod_ (I cannot select or delete by name because there are 200 variables in the data frame) Can this be done?

thank

+4
source share
3 answers

Use startswithfor mask and then remove columns with locand boolean indexing:

df = df.loc[:, ~df.columns.str.startswith('Prod')]
print (df)
   State  Region
1      1       1
+8
source

First select all the columns you want to delete:

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

Drop all:

df.drop(unwanted, axis=1, inplace=True)
+3
source

we can also use negative regEx:

In [269]: df.filter(regex=r'^(?!Prod_).*$')
Out[269]:
   State  Region
1      1       1
+1
source

Source: https://habr.com/ru/post/1676550/


All Articles