Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the fast way to drop columns in pandas dataframe from a list of column names [duplicate]

I'm trying to figure out the fastest way to drop columns in df using a list of column names. this is a fancy feature reduction technique. This is what I am using now, and it is taking forever. Any suggestions are highly appreciated.

    important2=(important[:-(len(important)-500)]) 
    for i in important:
        if i in important2:
            pass
        else:
            df_reduced.drop(i, axis=1, inplace=True)
    df_reduced.head()
like image 464
lrn2code Avatar asked Nov 15 '16 02:11

lrn2code


People also ask

How you can drop multiple columns of a pandas DataFrame at once using a list of column names?

We can use Pandas drop() function to drop multiple columns from a dataframe. Pandas drop() is versatile and it can be used to drop rows of a dataframe as well. To use Pandas drop() function to drop columns, we provide the multiple columns that need to be dropped as a list.

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.


1 Answers

use a list containing the columns to be dropped:

good_bye_list = ['column_1', 'column_2', 'column_3']
df_reduced.drop(good_bye_list, axis=1, inplace=True)
like image 120
ℕʘʘḆḽḘ Avatar answered Sep 28 '22 01:09

ℕʘʘḆḽḘ