Search code examples
pythonpandasdataframedrop

Dropping columns before a matching string in the following column


Is there direct possibility to drop all columns before a matching string in a pandas Dataframe. For eg. if my column 8 contains a string 'Matched' I want to drop columns 0 to 7 ?


Solution

  • Well, you did not give any information where and how to look for 'Matched', but let's say that integer col_num contains the number of the matched column:

    col_num = np.where(df == 'Matched')[1][0]
    df.drop(columns=df.columns[0:col_num],inplace=True)
    

    will do the drop