• 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 / How To Delete Rows in Pandas Dataframe

How To Delete Rows in Pandas Dataframe

March 16, 2021 by cmdlinetips

Pandas make it easy to delete rows of a dataframe. There are multiple way to delete rows or select rows from a dataframe. In this post, we will see how to use drop() function to drop rows in Pandas by index names or index location..

Pandas drop() function can also be used drop or delete columns from Pandas dataframe. Therefore, to drop rows from a Pandas dataframe, we need to specify the row indexes that need to be dropped with axis=0 or axis=”index” argument. Here, axis=0 or axis=”index” argument specifies we want to drop rows instead of dropping columns.

How to drop a row in Pandas?

Let us load Pandas and Seaborn load Penguin data set to illustrate how to delete one or more rows from the dataframe.

 
import seaborn as sns
import pandas as pd

We will be using just a few rows from the penguins data.

 
df = (sns.load_dataset("penguins").
      head())

Here is our toy data for learning how to delete rows by using index name. Note that indices of the toy dataframe is numeric.

 
df
	species	island	bill_length_mm	bill_depth_mm	flipper_length_mm	body_mass_g	sex
0	Adelie	Torgersen	39.1	18.7	181.0	3750.0	Male
1	Adelie	Torgersen	39.5	17.4	186.0	3800.0	Female
2	Adelie	Torgersen	40.3	18.0	195.0	3250.0	Female
3	Adelie	Torgersen	NaN	NaN	NaN	NaN	NaN
4	Adelie	Torgersen	36.7	19.3	193.0	3450.0	Female

Let us change the index to contain some text instead of numbers in order.

 
# assign index names to dataframe
df.index=["one","two","three","four","five"]

We can see the index is not numbers.

 
df

species	island	bill_length_mm	bill_depth_mm	flipper_length_mm	body_mass_g	sex
one	Adelie	Torgersen	39.1	18.7	181.0	3750.0	Male
two	Adelie	Torgersen	39.5	17.4	186.0	3800.0	Female
three	Adelie	Torgersen	40.3	18.0	195.0	3250.0	Female
four	Adelie	Torgersen	NaN	NaN	NaN	NaN	NaN
five	Adelie	Torgersen	36.7	19.3	193.0	3450.0	Female

How to Drop one row by index name?

To delete a row from a dataframe, we specify the index name and also use “axis=0” argument. In this example, we drop row with name “one”.

 
df.drop("one",axis=0)

	species	island	bill_length_mm	bill_depth_mm	flipper_length_mm	body_mass_g	sex
two	Adelie	Torgersen	39.5	17.4	186.0	3800.0	Female
three	Adelie	Torgersen	40.3	18.0	195.0	3250.0	Female
four	Adelie	Torgersen	NaN	NaN	NaN	NaN	NaN
five	Adelie	Torgersen	36.7	19.3	193.0	3450.0	Female

Another way to specify we want to delete a row not a column is to use axis=”index” argument instead of axis=0. Again, we drop row with name “one”.

 
df.drop("one",axis="index")

	species	island	bill_length_mm	bill_depth_mm	flipper_length_mm	body_mass_g	sex
two	Adelie	Torgersen	39.5	17.4	186.0	3800.0	Female
three	Adelie	Torgersen	40.3	18.0	195.0	3250.0	Female
four	Adelie	Torgersen	NaN	NaN	NaN	NaN	NaN
five	Adelie	Torgersen	36.7	19.3	193.0	3450.0	Female

How to Delete Multiple Rows by index names?

In order to delete multiple rows, we need to specify the index names as a list to Pandas drop() function. In this example, we drop the first two rows by specifying their names in a list.

 
df.drop(["one","two"],axis="index")

	species	island	bill_length_mm	bill_depth_mm	flipper_length_mm	body_mass_g	sex
three	Adelie	Torgersen	40.3	18.0	195.0	3250.0	Female
four	Adelie	Torgersen	NaN	NaN	NaN	NaN	NaN
five	Adelie	Torgersen	36.7	19.3	193.0	3450.0	Female

How to Delete Multiple Rows by their locations?

Sometimes, we might want to delete one or multiple rows by their location instead of their index names. To delete by their location, we can use subsetted index as shown here.

df.drop(df.index[[0,1]])


species	island	bill_length_mm	bill_depth_mm	flipper_length_mm	body_mass_g	sex
three	Adelie	Torgersen	40.3	18.0	195.0	3250.0	Female
four	Adelie	Torgersen	NaN	NaN	NaN	NaN	NaN
five	Adelie	Torgersen	36.7	19.3	193.0	3450.0	Female 

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 ThumbnailPandas filter(): Select Columns and Rows by Labels in a Dataframe Missing Values Count with isna()How To Get Number of Missing Values in Each Column in Pandas Default ThumbnailPandas value_counts: How To Get Counts of Unique Variables in a Dataframe? How to Code Character Variable as Integers with Pandas?How To Code a Character Variable into Integer in Pandas

Filed Under: Pandas 101, Python Tagged With: Pandas 101, Python

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