Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a column from a Pandas DataFrame

When deleting a column in a DataFrame I use:

del df['column_name'] 

And this works great. Why can't I use the following?

del df.column_name 

Since it is possible to access the column/Series as df.column_name, I expected this to work.

like image 433
John Avatar asked Nov 16 '12 06:11

John


People also ask

How do I remove a specific column from a DataFrame in Python?

To delete rows and columns from DataFrames, Pandas uses the “drop” function. To delete a column, or multiple columns, use the name of the column(s), and specify the “axis” as 1. Alternatively, as in the example below, the 'columns' parameter has been added in Pandas which cuts out the need for 'axis'.

How do I remove a column from a DataFrame list?

DataFrame. drop() method removes the column/columns from the DataFrame, by default it doesn't remove on the existing DataFrame instead it returns a new DataFrame after dropping the columns specified with the drop method. In order to remove columns on the existing DataFrame object use inplace=True param.


1 Answers

The best way to do this in Pandas is to use drop:

df = df.drop('column_name', 1) 

where 1 is the axis number (0 for rows and 1 for columns.)

To delete the column without having to reassign df you can do:

df.drop('column_name', axis=1, inplace=True) 

Finally, to drop by column number instead of by column label, try this to delete, e.g. the 1st, 2nd and 4th columns:

df = df.drop(df.columns[[0, 1, 3]], axis=1)  # df.columns is zero-based pd.Index 

Also working with "text" syntax for the columns:

df.drop(['column_nameA', 'column_nameB'], axis=1, inplace=True) 

Note: Introduced in v0.21.0 (October 27, 2017), the drop() method accepts index/columns keywords as an alternative to specifying the axis.

So we can now just do:

df = df.drop(columns=['column_nameA', 'column_nameB']) 
like image 157
LondonRob Avatar answered Oct 01 '22 08:10

LondonRob