46

I know how to drop columns from a data frame using Python. But for my problem the data set is vast, the columns I want to drop are grouped together or are basically singularly spread out across the column heading axis. Is there a shorter way to slice or drop all the columns with fewer lines of code rather than to write it out like how I have done. The way I have done it here works but I would like a more summarized way.

The flight_data_copy_final is the variable in which it should be stored.

Here's my code:

from IPython.display import display

flight_data_copy_version1 = flight_data_copy.drop(flight_data_copy.ix[:,"Year": "FlightDate"].columns, axis=1)
flight_data_copy_version2 = flight_data_copy_version1.drop("TailNum", axis=1)
flight_data_copy_version3 = flight_data_copy_version2.drop("OriginStateFips", axis=1)
flight_data_copy_version4 = flight_data_copy_version3.drop("DestStateFips", axis=1)
flight_data_copy_version5 = flight_data_copy_version4.drop("Diverted", axis=1)
flight_data_copy_version6 = flight_data_copy_version5.drop("Flights", axis=1)
flight_data_copy_final = flight_data_copy.drop(flight_data_copy_version6.ix[:,"FirstDepTime":].columns, axis=1)

print (display (flight_data_copy_final))
2
  • 13
    you can do it this way: df.drop(['col1','col2','col5','colN'], 1) Commented Nov 2, 2016 at 20:19
  • 5
    You don't need to assign so many intermediate variables. You could do df.drop('col1', axis=1).drop('col2', axis=1)..... Or better drop all cols in one operation, and possibly inplace with df.drop(['col1','col2','col5','colN'], axis=1, inplace=True) Commented Nov 2, 2016 at 20:22

2 Answers 2

88

To delete multiple columns at the same time in pandas, you could specify the column names as shown below. The option inplace=True is needed if one wants the change affected column in the same dataframe. Otherwise remove it.

flight_data_copy.drop(['TailNum', 'OriginStateFips', 
                'DestStateFips', 'Diverted'], axis=1, inplace=True)

Source: Python Pandas - Deleting multiple series from a data frame in one command

Sign up to request clarification or add additional context in comments.

Comments

23
    df.drop(columns=['col_1', 'col_2','col_N'])

In my case, I solve it like thisenter image description here

3 Comments

Why do you prefer this over the accepted answer?
I like this answer, the code is more readable using columns= instead of axis=1. I think both are equivalent though.
The accepted answer gave me errors when I used it with a list of column names - this small change doesn't give an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.