Pandas DataFrame DataFrame.dropna() 函式

Minahil Noor 2023年1月30日
  1. pandas.DataFrame.dropna() 語法
  2. 示例程式碼:DataFrame.dropna() 刪除行
  3. 示例程式碼:DataFrame.dropna() 刪除列
  4. 示例程式碼:DataFrame.dropna()how=all
  5. 示例程式碼:DataFrame.dropna() 與指定的子集或閾值
  6. 示例程式碼:DataFrame.dropna()inplace=True
Pandas DataFrame DataFrame.dropna() 函式

pandas.DataFrame.dropna() 函式通過丟棄包含空值的行或列,從 DataFrame 中刪除空值(缺失值)。

NaNNot a Number)和 NaTNot a Time)代表空值。DataFrame.dropna() 檢測這些值並相應地過濾 DataFrame

pandas.DataFrame.dropna() 語法

DataFrame.dropna(axis, how, thresh, subset, inplace)

引數

axis 它決定軸是行還是列。
如果它是 0 或'index',那麼它將刪除包含缺失值的行。
如果它是 1 或'column',那麼它將刪除包含缺失值的列。預設情況下,它的值是 0
how 這個引數決定函式如何刪除行或列。它只接受兩個字串,可以是 allall。預設情況下,它被設定為 any
any - 如果行或列中有任何空值,就會刪除它。
all - 如果行或列中缺少所有值,則放棄該行或列
thresh 它是一個整數,指定了防止行或列丟失的非缺失值的最少數量
subset 它是一個陣列,其中有行或列的名稱,用於指定刪除程式
inplace 它是一個布林值,如果設定為 True,將就地改變呼叫者 DataFrame。預設情況下,它的值是 False

返回值

它根據傳遞的引數返回一個過濾後的 DataFrame,其中包含刪除的行或列。

示例程式碼:DataFrame.dropna() 刪除行

預設情況下,軸為 0,即行,所以所有的輸出都有行掉。

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: None, 2: 80,3: None, 4: 95},
                    'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                    'Obtained Marks': {0: None, 1: 75, 2: 82, 3: 64, 4: None}})
print(dataframe)

示例 DataFrame 如下。

   Attendance    Name  Obtained Marks
0        60.0  Olivia             NaN
1         NaN    John            75.0
2        80.0   Laura            82.0
3         NaN     Ben            64.0
4        95.0   Kevin             NaN

這個函式的所有引數都是可選的。如果我們不傳遞任何引數,那麼函式將丟棄所有包含一個空值的行。

import pandas as pd

dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: None, 2: 80, 3: None, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: None, 1: 75, 2: 82, 3: 64, 4: None},
    }
)
dataframe1 = dataframe.dropna()
print(dataframe1)

輸出:

   Attendance   Name  Obtained Marks
2        80.0  Laura            82.0

丟棄所有包含一個缺失值的行。

示例程式碼:DataFrame.dropna() 刪除列

import pandas as pd

dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: None, 2: 80, 3: None, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: None, 1: 75, 2: 82, 3: 64, 4: None},
    }
)
dataframe1 = dataframe.dropna(axis=1)

print(dataframe1)

輸出:

     Name
0  Olivia
1    John
2   Laura
3     Ben
4   Kevin

因為我們在 DataFrame.dropna() 方法中設定了 axis=1,所以它刪除了所有包含一個缺失值的列。

示例程式碼:DataFrame.dropna()how=all

import pandas as pd

dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: None, 2: 80, 3: None, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: None, 1: 75, 2: 82, 3: 64, 4: None},
    }
)

dataframe1 = dataframe.dropna(axis=1, how="all")
print(dataframe1)

輸出:

   Attendance    Name  Obtained Marks
0        60.0  Olivia             NaN
1         NaN    John            75.0
2        80.0   Laura            82.0
3         NaN     Ben            64.0
4        95.0   Kevin             NaN

包含缺失值的行沒有被刪除,因為 how 引數的值被設定為 all,這意味著該行的所有值都應該是空的。

如果在指定的軸上缺少所有的值,那麼 DataFrame.dropna() 方法會丟棄該軸,即使 how 被設定為 all

import pandas as pd

dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: None, 2: 80, 3: None, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: None, 1: None, 2: None, 3: None, 4: None},
    }
)

print(dataframe)
print("--------")
dataframe1 = dataframe.dropna(axis=1, how="all")
print(dataframe1)

輸出:

   Attendance    Name Obtained Marks
0        60.0  Olivia           None
1         NaN    John           None
2        80.0   Laura           None
3         NaN     Ben           None
4        95.0   Kevin           None   Attendance    Name
0        60.0  Olivia
1         NaN    John
2        80.0   Laura
3         NaN     Ben
4        95.0   Kevin

示例程式碼:DataFrame.dropna() 與指定的子集或閾值

import pandas as pd

dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: None, 2: 80, 3: None, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: None, 1: 75, 2: 82, 3: 64, 4: None},
    }
)

dataframe1 = dataframe.dropna(thresh=3)
print(dataframe1)

輸出:

   Attendance   Name  Obtained Marks
2        80.0  Laura            82.0

thresh 的值是 3,這意味著為了防止掉落,至少需要 3 個非空值。

我們也可以指定 subset

import pandas as pd

dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: None, 2: 80, 3: None, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: None, 1: 75, 2: 82, 3: 64, 4: None},
    }
)

dataframe1 = dataframe.dropna(subset=["Attendance", "Name"])
print(dataframe1)

輸出:

   Attendance    Name  Obtained Marks
0        60.0  Olivia             NaN
2        80.0   Laura            82.0
4        95.0   Kevin             NaN

根據 AttendanceName 列,刪除缺失值的行。如果只有其他列中的值比如這裡的 Obtained Marks 列有缺失值,它就不會刪除記錄。

示例程式碼:DataFrame.dropna()inplace=True

DataFrame.dropna() 如果 inplace 被設定為 True,則呼叫者 DataFrame 就地改變。

import pandas as pd

dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: None, 2: 80, 3: None, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: None, 1: 75, 2: 82, 3: 64, 4: None},
    }
)
dataframe1 = dataframe.dropna(inplace=True)
print(dataframe1)

輸出:

None

該引數對呼叫者 DataFrame 進行了原地修改,返回 None

相關文章 - Pandas DataFrame