delete all columns except one from pandas dataframe

Pandas – Delete All Columns Except Some Columns

In this tutorial, we will look at how to delete all the columns of a pandas dataframe except one (or some) specific column(s) with the help of some examples.

How to delete all columns except one (or more) specific column(s)?

delete all columns except one from pandas dataframe

To delete all the columns except some specific ones from a pandas dataframe, you can filter the dataframe such that it contains only those specific columns.

The are multiple ways to filter a dataframe for specific columns in Pandas –

  • You can use the square brackets notation to select the columns that you want. For example, if you want to retain only columns “Col1” and “Col2” from the dataframe df, use df[['Col1', 'Col2']].
  • Using the pandas dataframe filter() function.
  • Using .loc property to keep only the columns you want.

Examples

Let’s now look at some examples of using the above methods to remove all columns except some from a pandas dataframe.

For this tutorial, we will be using the Pokemon dataset to create our dataframe and show the usage of the different methods.

import pandas as pd

# load the pokemon dataset
df = pd.read_csv('data/Pokemon.csv')
# display the dataframe head
df.head()

Output:

pokemon dataframe first five rows

Here, we loaded the Pokemon dataset as a dataframe using the read_csv() function in pandas. You can see that the dataframe has 13 columns with different information and attributes about a pokemon.

Example 1 – Remove all columns except some using square bracket notation

Let’s remove all the columns except the “Name” column from the above dataframe using the square bracket notation.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

In this method, we use the list of columns we want to select (or retain) inside a pair of square brackets with the dataframe.

# keep only the "Name" column
df = df[["Name"]]
# display the dataframe head
df.head()

Output:

pokemon dataframe with only "Name" column

You can see that the resulting dataframe contains only the “Name” column.

You can similarly use this method to remove all the columns except some specific ones. For example, let’s reload the dataset and remove all the columns except “Name” and “Type 1”.

# load the pokemon dataset
df = pd.read_csv('data/Pokemon.csv')
# keep only the "Name" and "Type 1" columns
df = df[["Name", "Type 1"]]
# display the dataframe head
df.head()

Output:

pokemon dataframe with "Name" and "Type 1" columns

The dataframe now has only the “Name” and “Type 1” columns.

Example 2 – Using the filter() method

The pandas dataframe filter() method is used to filter a dataframe on rows and/or columns.

Pass the list of column names you want to filter (or retain) as an argument to the filter() method (it filters columns by default).

Let’s take the same use case from above. We’ll load the entire pokemon dataset and then remove all the columns except “Name” and “Type 1”.

# load the pokemon dataset
df = pd.read_csv('data/Pokemon.csv')
# keep only the "Name" and "Type 1" columns
df = df.filter(["Name", "Type 1"])
# display the dataframe head
df.head()

Output:

pokemon dataframe with "Name" and "Type 1" columns

We get the same result as above.

Example 3 – Retain columns using the .loc property

The loc dataframe property is used to slice (or filter) a dataframe based on its row and column labels.

Let’s take the same use case from the above examples. We’ll load the entire pokemon dataset and then remove all the columns except “Name” and “Type 1”.

# load the pokemon dataset
df = pd.read_csv('data/Pokemon.csv')
# keep only the "Name" and "Type 1" columns
df = df.loc[:, ["Name", "Type 1"]]
# display the dataframe head
df.head()

Output:

pokemon dataframe with "Name" and "Type 1" columns

We get the same result as above.

Summary

In this tutorial, we looked at some methods to delete all the columns except some (one or more) columns from a pandas dataframe by filtering the dataframe. The following are the key takeaways –

  • You can use the square bracket notation to select (or retain) the specific columns you want.
  • Alternatively, you can use the pandas filter() function to filter the dataframe for specific columns.
  • You can also use the dataframe .loc property to filter a dataframe using column names.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top