Pandas DataFrame 刪除某行

Suraj Joshi 2023年1月30日
  1. pandas.DataFrame.drop() 方法中按索引刪除行
  2. 根據 Pandas DataFrame 中某一列的值來刪除行
Pandas DataFrame 刪除某行

本教程說明了如何使用 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() 方法刪除這些行。

作者: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

相關文章 - Pandas DataFrame Row