pandas dataframe 删除某行-ag捕鱼王app官网

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图