Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping Multiple Columns from a data frame using Python

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))
like image 866
Deepak M Avatar asked Nov 02 '16 20:11

Deepak M


People also ask

How do I drop multiple columns from a DataFrame in Python?

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 you drop few columns in a data frame?

pandas drop() method remove the column by name and index from the DataFrame, by default it doesn't remove on the existing DataFrame instead it returns a new DataFrame without the columns specified with the drop method. In order to remove columns on the existing DataFrame object use inplace=True param.

How do you delete multiple columns in Python?

You can delete one or multiple columns of a DataFrame. To delete or remove only one column from Pandas DataFrame, you can use either del keyword, pop() function or drop() function on the dataframe. To delete multiple columns from Pandas Dataframe, use drop() function on the dataframe.

How do you drop unwanted columns in Python?

To drop a single column or multiple columns from pandas dataframe in Python, you can use `df. drop` and other different methods. During many instances, some columns are not relevant to your analysis.


2 Answers

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

like image 83
Prasanth Regupathy Avatar answered Sep 30 '22 10:09

Prasanth Regupathy


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

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

like image 30
Md.Rakibuz Sultan Avatar answered Sep 30 '22 11:09

Md.Rakibuz Sultan