迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

Pandas DataFrame 删除某行

作者:迹忆客 最近更新:2024/04/23 浏览次数:

本教程说明了如何使用 pandas.DataFrame.drop() 方法在 Pandas 中删除行。

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)

输出:

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

我们将使用 kgp_df DataFrame 来解释如何从 Pandas DataFrame 中删除行。


pandas.DataFrame.drop() 方法中按索引删除行

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)

输出:

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

kgp_df DataFrame 中删除索引为 0 和 2 的行。索引 0 和 2 的行对应 DataFrame 中的第一行和第三行,因为索引是从 0 开始的。

我们也可以使用 DataFrame 的索引来删除这些行,而不是使用默认的索引。

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)

输出:

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

它从 DataFrame 中删除索引 AC 的行,或者第一行和第三行。

我们将要删除的行的索引列表传递给 drop() 方法来删除相应的行。


根据 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)

输出:

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

它将删除所有年龄小于或等于 33 岁的行。

我们首先找到所有年龄小于或等于 33 岁的行的索引,然后使用 drop() 方法删除这些行。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:Python

Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

Pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:Python

本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。

Pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:Python

本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便