JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Pandas DataFrame Delete a row

Author:JIYIK Last Updated:2025/04/12 Views:

This tutorial explains how to pandas.DataFrame.drop()delete rows in Pandas using the method.

import pandas as pd

kgp_df = pd.DataFrame(
    {
        "Name": ["Himansh", "Prateek", "Abhishek", "Vidit", "Anupam"],
        "Age": [30, 33, 35, 30, 30],
        "Weight(KG)": [75, 75, 80, 70, 73],
    }
)
print("The KGP DataFrame is:")
print(kgp_df)

Output:

The KGP DataFrame is:
       Name  Age  Weight(KG)
0   Himansh   30          75
1   Prateek   33          75
2  Abhishek   35          80
3     Vidit   30          70
4    Anupam   30          73

We will use kgp_dfDataFrame to explain how to delete rows from a Pandas DataFrame.


pandas.DataFrame.drop()Delete rows by index in the method

import pandas as pd

kgp_df = pd.DataFrame(
    {
        "Name": ["Himansh", "Prateek", "Abhishek", "Vidit", "Anupam"],
        "Age": [30, 33, 35, 30, 30],
        "Weight(KG)": [75, 75, 80, 70, 73],
    }
)

rows_dropped_df = kgp_df.drop(kgp_df.index[[0, 2]])

print("The KGP DataFrame is:")
print(kgp_df, "\n")

print("The KGP DataFrame after dropping 1st and 3rd DataFrame is:")
print(rows_dropped_df)

Output:

The KGP DataFrame is:
       Name  Age  Weight(KG)
0   Himansh   30          75
1   Prateek   33          75
2  Abhishek   35          80
3     Vidit   30          70
4    Anupam   30          73

The KGP DataFrame after dropping 1st and 3rd DataFrame is:
      Name  Age  Weight(KG)
1  Prateek   33          75
3    Vidit   30          70
4   Anupam   30          73

Remove the rows with index 0 and 2 from kgp_dfthe DataFrame. The rows with index 0 and 2 correspond to the first and third rows in the DataFrame because indexing starts at 0.

We can also use the DataFrame's index to remove the rows instead of using the default index.

import pandas as pd

kgp_idx = ["A", "B", "C", "D", "E"]
kgp_df = pd.DataFrame(
    {
        "Name": ["Himansh", "Prateek", "Abhishek", "Vidit", "Anupam"],
        "Age": [30, 33, 35, 30, 30],
        "Weight(KG)": [75, 75, 80, 70, 73],
    },
    index=kgp_idx,
)

rows_dropped_df = kgp_df.drop(["A", "C"])

print("The KGP DataFrame is:")
print(kgp_df, "\n")

print("The KGP DataFrame after dropping 1st and 3rd DataFrame is:")
print(rows_dropped_df)

Output:

The KGP DataFrame is:
       Name  Age  Weight(KG)
A   Himansh   30          75
B   Prateek   33          75
C  Abhishek   35          80
D     Vidit   30          70
E    Anupam   30          73

The KGP DataFrame after dropping 1st and 3rd DataFrame is:
      Name  Age  Weight(KG)
B  Prateek   33          75
D    Vidit   30          70
E   Anupam   30          73

AIt removes the rows with index Cand , or the first and third rows, from the DataFrame .

We pass a list of indices of the rows to be deleted to drop()the method to delete the corresponding rows.


Delete rows based on the value of a column in a Pandas DataFrame

import pandas as pd

kgp_idx = ["A", "B", "C", "D", "E"]
kgp_df = pd.DataFrame(
    {
        "Name": ["Himansh", "Prateek", "Abhishek", "Vidit", "Anupam"],
        "Age": [31, 33, 35, 36, 34],
        "Weight(KG)": [75, 75, 80, 70, 73],
    },
    index=kgp_idx,
)

young_df_idx = kgp_df[kgp_df["Age"] <= 33].index
young_folks = kgp_df.drop(young_df_idx)

print("The KGP DataFrame is:")
print(kgp_df, "\n")

print("The DataFrame of folks with age less than or equal to 33 are:")
print(young_folks)

Output:

The KGP DataFrame is:
       Name  Age  Weight(KG)
A   Himansh   31          75
B   Prateek   33          75
C  Abhishek   35          80
D     Vidit   36          70
E    Anupam   34          73

The DataFrame of folks with age less than or equal to 33 are:
       Name  Age  Weight(KG)
C  Abhishek   35          80
D     Vidit   36          70
E    Anupam   34          73

It will delete all rows where the age is less than or equal to 33 years.

We first find the indices of all rows whose age is less than or equal to 33 and then drop()delete those rows using the method.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Finding the installed version of Pandas

Publish Date:2025/04/12 Views:190 Category:Python

Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc

KeyError in Pandas

Publish Date:2025/04/12 Views:81 Category:Python

This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest

Grouping and Sorting in Pandas

Publish Date:2025/04/12 Views:90 Category:Python

This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies

Plotting Line Graph with Data Points in Pandas

Publish Date:2025/04/12 Views:65 Category:Python

Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data. Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize

Converting Timedelta to Int in Pandas

Publish Date:2025/04/12 Views:123 Category:Python

This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .

Pandas fill NaN values

Publish Date:2025/04/12 Views:93 Category:Python

This tutorial explains how we can use DataFrame.fillna() the method to fill NaN values ​​with specified values. We will use the following DataFrame in this article. import numpy as np import pandas as pd roll_no = [ 501 , 502 , 503 , 50

Pandas Convert String to Number

Publish Date:2025/04/12 Views:147 Category:Python

This tutorial explains how to pandas.to_numeric() convert string values ​​of a Pandas DataFrame into numeric type using the method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 , 103 , 343 , 565 ], "Name" :

How to Change the Data Type of a Column in Pandas

Publish Date:2025/04/12 Views:139 Category:Python

We will look at methods for changing the data type of columns in a Pandas Dataframe, as well as options like to_numaric , , as_type and infer_objects . We will also discuss how to to_numaric use downcasting the option in . to_numeric Method

Get the first row of Dataframe Pandas

Publish Date:2025/04/12 Views:78 Category:Python

This tutorial explains how to use the get_first_row pandas.DataFrame.iloc attribute and pandas.DataFrame.head() get_first_row method from a Pandas DataFrame. We will use the following DataFrame in the following example to explain how to get

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial