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
Use startswithfor mask and then remove columns with locand boolean indexing:
startswith
loc
boolean indexing
df = df.loc[:, ~df.columns.str.startswith('Prod')] print (df) State Region 1 1 1
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)
we can also use negative regEx:
In [269]: df.filter(regex=r'^(?!Prod_).*$') Out[269]: State Region 1 1 1
Source: https://habr.com/ru/post/1676550/More articles:Почему время выполнения ребенка является отрицательным? - cLeap year and not a leap year in separate tables - htmlFilter nan rows in specific column - pythonWhy is Array.forEach running slower than the for () loop in Javascript? - performanceHow to make the drum handle of LESS files? - node.jslike hashmap using hashtable to store and retrieve an object - javaHow to listen to the entire queue in laravel? - phpAdding a Lambda expression and working with Entity Framework - c #Linux heap allocation - cColored characters in the HTML header - cssAll Articles