How to delete column in pandas

How to delete column in pandas

pandas.DataFrame.dropВ¶

Drop specified labels from rows or columns.

Remove rows or columns by specifying label names and corresponding axis, or by specifying directly index or column names. When using a multi-index, labels on different levels can be removed by specifying the level. See the user guide for more information about the now unused levels.

Parameters labels single label or list-like

Index or column labels to drop. A tuple will be used as a single label and not treated as a list-like.

Whether to drop labels from the index (0 or ‘index’) or columns (1 or ‘columns’).

index single label or list-like

Alternative to specifying axis ( labels, axis=0 is equivalent to index=labels ).

columns single label or list-like

Alternative to specifying axis ( labels, axis=1 is equivalent to columns=labels ).

level int or level name, optional

For MultiIndex, level from which the labels will be removed.

inplace bool, default False

If False, return a copy. Otherwise, do operation inplace and return None.

errors <‘ignore’, ‘raise’>, default ‘raise’

If ‘ignore’, suppress error and only existing labels are dropped.

Returns DataFrame or None

If any of the labels is not found in the selected axis.

Label-location based indexer for selection by label.

Return DataFrame with labels on given axis omitted where (all or any) data are missing.

Return DataFrame with duplicate rows removed, optionally only considering certain columns.

Return Series with specified index labels removed.

How to delete a column from a data frame with pandas?

How can I delete the id column from the above data frame?. I tried the following:

But it raises this exception:

3 Answers 3

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

df.drop(colname, axis=1) (or del df[colname] ) is the correct method to use to delete a column.

If a ValueError is raised, it means the column name is not exactly what you think it is.

Check df.columns to see what Pandas thinks are the names of the columns.

The best way to delete a column in pandas is to use drop:

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:

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

An exception would be raised answer was based on @LondonRob’s answer and left here to help future visitors of this page.

To actually delete the column

del df[‘id’] or df.drop(‘id’, 1) should have worked if the passed column matches exactly

However, if you don’t need to delete the column then you can just select the column of interest like so:

If you never wanted it in the first place then you pass a list of cols to read_csv as a param usecols :

How to Delete Column(s) of Pandas DataFrame?

Pandas DataFrame – Delete Column(s)

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.

Example 1: Delete a column using del keyword

In this example, we will create a DataFrame and then delete a specified column using del keyword. The column is selected for deletion, using the column label.

Python Program

Output

We have deleted chemistry column from the dataframe.

Example 2: Delete a column using pop() function

In this example, we will create a DataFrame and then use pop() function on the dataframe to delete a specific column. The column is selected for deletion, using the column label.

Python Program

Output

We have deleted chemistry column from the dataframe.

Example 3: Delete a column using drop() function

In this example, we will use drop() function on the dataframe to delete a specific column. We use column label to select a column for deletion.

Python Program

Output

How to delete column in pandas. Смотреть фото How to delete column in pandas. Смотреть картинку How to delete column in pandas. Картинка про How to delete column in pandas. Фото How to delete column in pandas

Example 4: Delete multiple columns using drop() function

In this example, we will use drop() function on the dataframe to delete multiple columns. We use array of column labels to select columns for deletion.

Python Program

Output

How to delete column in pandas. Смотреть фото How to delete column in pandas. Смотреть картинку How to delete column in pandas. Картинка про How to delete column in pandas. Фото How to delete column in pandas

Summary

In this Pandas Tutorial, we learned how to delete a column from Pandas DataFrame using del keyword, pop() method and drop() method, with the help of well detailed Python Examples.

Delete a column from a Pandas DataFrame

To delete a column in a DataFrame, I can successfully use:

But why can’t I use the following?

How to delete column in pandas. Смотреть фото How to delete column in pandas. Смотреть картинку How to delete column in pandas. Картинка про How to delete column in pandas. Фото How to delete column in pandas

How to delete column in pandas. Смотреть фото How to delete column in pandas. Смотреть картинку How to delete column in pandas. Картинка про How to delete column in pandas. Фото How to delete column in pandas

20 Answers 20

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

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

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:

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

Also working with «text» syntax for the columns:

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:

How to delete column in pandas. Смотреть фото How to delete column in pandas. Смотреть картинку How to delete column in pandas. Картинка про How to delete column in pandas. Фото How to delete column in pandas

As you’ve guessed, the right syntax is

It’s difficult to make del df.column_name work simply as the result of syntactic limitations in Python. del df[name] gets translated to df.__delitem__(name) under the covers by Python.

How to delete column in pandas. Смотреть фото How to delete column in pandas. Смотреть картинку How to delete column in pandas. Картинка про How to delete column in pandas. Фото How to delete column in pandas

This will delete one or more columns in-place. Note that inplace=True was added in pandas v0.13 and won’t work on older versions. You’d have to assign the result back in that case:

How to delete column in pandas. Смотреть фото How to delete column in pandas. Смотреть картинку How to delete column in pandas. Картинка про How to delete column in pandas. Фото How to delete column in pandas

Drop by index

Delete first, second and fourth columns:

Delete first column:

There is an optional parameter inplace so that the original data can be modified without creating a copy.

Popped

Delete column column-name :

Examples:

df.drop(df.columns[[0]], axis=1, inplace=True) print df :

three = df.pop(‘three’) print df :

How to delete column in pandas. Смотреть фото How to delete column in pandas. Смотреть картинку How to delete column in pandas. Картинка про How to delete column in pandas. Фото How to delete column in pandas

How to delete column in pandas. Смотреть фото How to delete column in pandas. Смотреть картинку How to delete column in pandas. Картинка про How to delete column in pandas. Фото How to delete column in pandas

The actual question posed, missed by most answers here is:

At first we need to understand the problem, which requires us to dive into Python magic methods.

As Wes points out in his answer, del df[‘column’] maps to the Python magic method df.__delitem__(‘column’) which is implemented in Pandas to drop the column.

However, as pointed out in the link above about Python magic methods:

In fact, __del__ should almost never be used because of the precarious circumstances under which it is called; use it with caution!

You could argue that del df[‘column_name’] should not be used or encouraged, and thereby del df.column_name should not even be considered.

Example Problem

What if I define a column in a dataframe called «dtypes» or «columns»?

Then assume I want to delete these columns.

del df.dtypes would make the __delattr__ method confused as if it should delete the «dtypes» attribute or the «dtypes» column.

Architectural questions behind this problem

Pandas answers:

Pro tip:

Don’t use df.column_name. It may be pretty, but it causes cognitive dissonance.

Zen of Python quotes that fits in here:

There are multiple ways of deleting a column.

Columns are sometimes attributes but sometimes not.

Special cases aren’t special enough to break the rules.

Does del df.dtypes delete the dtypes attribute or the dtypes column?

In the face of ambiguity, refuse the temptation to guess.

How To Delete A Column In Pandas

Deleting columns from pandas DataFrames

How to delete column in pandas. Смотреть фото How to delete column in pandas. Смотреть картинку How to delete column in pandas. Картинка про How to delete column in pandas. Фото How to delete column in pandas

Introduction

Deleting columns from pandas DataFrames is a fairly common task. In today’s short guide we are going to explore how you can delete specific column(s) by name. More specifically, we are going to discuss how to delete columns using:

Additionally, we will discuss how to delete columns by specified their index rather than their name.

First, let’s create an example pandas DataFrame that we’ll use throughout this guide in order to demonstrate a few concepts.

Deleting columns using del

If you want to delete a specific column the first option you have is to call del as shown below:

This approach will only work only if you wish to delete a single column. If you need to delete multiple column in one go, read the following section.

Additionally, it’s important to note that del df.colC won’t work! The syntax must be identical to the one shown in the example above.

pandas.DataFrame.drop method is used to delete the specified labels from either rows or columns.

In order to delete columns, you need to specify axis=1 as shown below:

Another option is pandas.DataFrame.pop that returns the deleted column and finally deletes it from the original DataFrame.

will return column colB that we want to get deleted from the DataFrame:

and we can verify that the column has indeed been deleted from the original frame:

Deleting columns by index

In the sections above, we explored how to delete specific columns by their name. However, you may also need to delete a particular column by referencing its index instead.

To do so, you can use pandas.DataFrame.drop method. Assuming that we want to drop columns colA and colC with indices 0 and 2 respectively, we can do so as shown below:

Final Thoughts

In today’s short guide we explored how to delete specific column(s) in a pandas DataFrame in various ways either by name or by index.

You may also be interested in reading about how to rename columns of pandas DataFrames. If so make sure not to miss the article below.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *