• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Pandas DataFrame / Drop a Column in Pandas / How To Drop One or More Columns in Pandas Dataframe?

How To Drop One or More Columns in Pandas Dataframe?

April 6, 2018 by cmdlinetips

Often while working with a bigger pandas dataframe with multiple columns, one wants to drop a column or multiple columns from a pandas dataframe.
How To Drop Columns in Pandas?
How To Drop Columns in Pandas?

One typically drops columns, if the columns are not needed for further analysis. Pandas drop function allows you to drop/remove one or more columns from a dataframe.

Let us see some examples of dropping or removing columns from a real world data set. Let us load pandas and load gapminder data from a URL.

import pandas as pd
gapminder_url='https://bit.ly/2cLzoxH'
gapminder = pd.read_csv(gapminder_url)
gapminder.head()

Let us do some filtering to make the dataframe smaller just for the ease of illustrating the examples of using drop function in Pandas. After filtering, we will have a smaller dataframe with just four rows and six columns.

gapminder_ocean = gapminder[(gapminder.year >2000) &
                            (gapminder.continent== 'Oceania')]
gapminder_ocean.shape
gapminder_ocean

How To Drop a Single Column from a Dataframe?

To drop a single column from pandas dataframe, we need to provide the name of the column to be dropped as a list as an argument to drop function. Here, we have a list containing just one element, ‘pop’ variable. Pandas drop function can drop column or row. To specify we want to drop column, we need to provide axis=1 as another argument to drop function.

# pandas drop a column with drop function
gapminder_ocean.drop(['pop'], axis=1)

The resulting dataframe will have just five columns instead of six. The column containing pop variable is removed now.

	country	year	continent	lifeExp	gdpPercap
70	Australia	2002	Oceania	80.370	30687.75473
71	Australia	2007	Oceania	81.235	34435.36744
1102	New Zealand	2002	Oceania	79.110	23189.80135
1103	New Zealand	2007	Oceania	80.204	25185.00911

How To Drop Multiple Columns from a Dataframe?

Pandas’ drop function can be used to drop multiple columns as well. To drop or remove multiple columns, one simply needs to give all the names of columns that we want to drop as a list. Here is an example with dropping three columns from gapminder dataframe.

# pandas drop columns using list of column names
gapminder_ocean.drop(['pop', 'gdpPercap', 'continent'], axis=1)

Note that now the resulting data frame contains just three columns instead of six columns.

country	year	lifeExp
70	Australia	2002	80.370
71	Australia	2007	81.235
1102	New Zealand	2002	79.110
1103	New Zealand	2007	80.204

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Reset Index in Pandas Dataframe? Pandas Filter/Select Rows Based on Column ValuesHow To Filter Pandas Dataframe By Values of Column? Default ThumbnailHow To Drop Multiple Columns in Pandas Dataframe? Default ThumbnailHow to Drop Rows Based on a Column Value in Pandas Dataframe?

Filed Under: Drop a Column in Pandas, drop a row in Pandas, Pandas DataFrame, Pandas Drop, Pandas Tutorial, Python Tips Tagged With: Pandas Delete Columns, Pandas Drop, Python Tips

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version