透過標籤名稱或索引位置刪除 DataFrame 中的列
Pandas DataFrame 是一種由一系列實體組成的二維資料結構。它在數學資料分析中非常有用。資料以表格形式排列,每行都作為資料的一個例項。
Pandas DataFrame 非常特殊,因為它擁有眾多功能,使其成為一個非常強大的程式設計工具。DataFrame 中的每一列都代表一系列資訊,並帶有標籤。在本文中,我們將對這些列進行操作,並討論在 Pandas DataFrame 中刪除列的各種方法。
可以透過指定列名或使用其索引值來刪除單個或多個列。我們將瞭解這兩種方法,但首先我們必須準備一個數據集並生成一個 DataFrame。
建立 DataFrame
在建立 DataFrame 時,我們可以為我們的表格分配列名和行名。此過程很重要,因為它指定了“**標籤名稱**”和“**索引值**”。
在這裡,我們將 Pandas 庫匯入為“pd”,然後使用列表字典傳遞資料集。每個鍵代表一列資料,與其關聯的值以列表形式傳遞。我們使用 Pandas 的“**DataFrame()**”函式建立了 DataFrame。我們藉助“**index**”引數為 DataFrame 分配了行標籤。現在讓我們使用列名刪除列。
示例
import pandas as pd
dataset = {"Employee ID":["CIR45", "CIR12", "CIR18", "CIR50", "CIR28"], "Age":[25, 28, 27, 26, 25], "Salary":[200000, 250000, 180000, 300000, 280000], "Role":["Junior Developer", "Analyst", "Programmer", "Senior Developer", "HR"]}
dataframe = pd.DataFrame(dataset, index=["Nimesh", "Arjun", "Mohan", "Ritesh", "Raghav"])
print(dataframe)
輸出
Employee ID Age Salary Role Nimesh CIR45 25 200000 Junior Developer Arjun CIR12 28 250000 Analyst Mohan CIR18 27 180000 Programmer Ritesh CIR50 26 300000 Senior Developer Raghav CIR28 25 280000 HR
使用列名和 drop() 方法
生成 DataFrame 後,我們使用“**dataframe.drop**”方法從 DataFrame 中刪除“**Salary**”和“**Role**”列。我們將這些列名放在一個列表中。
我們指定“**axis**”值為 1,因為我們正在對列軸進行操作。最後,我們將此新的 DataFrame 儲存在一個變數“**colDrop**”中並打印出來。
示例
import pandas as pd
dataset = {"Employee ID":["CIR45", "CIR12", "CIR18", "CIR50", "CIR28"], "Age":[25, 28, 27, 26, 25], "Salary":[200000, 250000, 180000, 300000, 280000], "Role":["Junior Developer", "Analyst", "Programmer", "Senior Developer", "HR"]}
dataframe = pd.DataFrame(dataset, index=["Nimesh", "Arjun", "Mohan", "Ritesh", "Raghav"])
print(dataframe)
colDrop = dataframe.drop(["Role", "Salary"], axis=1)
print("After dropping the Role and salary column:")
print(colDrop)
輸出
Employee ID Age Salary Role
Nimesh CIR45 25 200000 Junior Developer
Arjun CIR12 28 250000 Analyst
Mohan CIR18 27 180000 Programmer
Ritesh CIR50 26 300000 Senior Developer
Raghav CIR28 25 280000 HR
After dropping the Role and salary column:
Employee ID Age
Nimesh CIR45 25
Arjun CIR12 28
Mohan CIR18 27
Ritesh CIR50 26
Raghav CIR28 25
使用索引值和 drop() 方法
我們可以使用索引位置來鎖定要刪除的列。
示例
在這裡,我們簡單地使用了“**dataframe.columns**”方法以及“dataframe.drop()”來指定要刪除的列的索引位置。我們傳遞了“[[2,3]]”引數以刪除“Salary”和“role”列。
現在我們已經討論了刪除列的兩種基本方法,讓我們討論一些擴充套件概念。
colDrop = dataframe.drop(dataframe.columns[[2, 3]], axis=1)
print("After dropping salary and role: -")
print(colDrop)
輸出
After dropping salary and role: -
Employee ID Age
Nimesh CIR45 25
Arjun CIR12 28
Mohan CIR18 27
Ritesh CIR50 26
Raghav CIR28 25
從 DataFrame 中刪除一系列列
在上面討論的示例中,我們只刪除了特定的列(Salary 和 Role),但眾所周知,Pandas 為程式設計師提供了許多功能,因此我們可以使用它來建立一系列要刪除的列。讓我們實現此邏輯。
使用 iloc() 函式
生成 DataFrame 後,我們使用“**iloc() 函式**”選擇一系列列並將其從 DataFrame 中刪除。“iloc()”函式對行和列都採用索引範圍。行的範圍設定為“[0:0]”,列的範圍設定為“[1:4]”。最後,我們使用“dataframe.drop()”方法刪除這些列。
示例
import pandas as pd
dataset = {"Employee ID":["CIR45", "CIR12", "CIR18", "CIR50", "CIR28"], "Age":[25, 28, 27, 26, 25], "Salary":[200000, 250000, 180000, 300000, 280000], "Role":["Junior Developer", "Analyst", "Programmer", "Senior Developer", "HR"]}
dataframe = pd.DataFrame(dataset, index=["Nimesh", "Arjun", "Mohan", "Ritesh", "Raghav"])
print(dataframe)
colDrop = dataframe.drop(dataframe.iloc[0:0, 1:4],axis=1)
print("Dropping a range of columns from 'Age' to 'Role' using iloc() function")
print(colDrop)
輸出
Employee ID Age Salary Role
Nimesh CIR45 25 200000 Junior Developer
Arjun CIR12 28 250000 Analyst
Mohan CIR18 27 180000 Programmer
Ritesh CIR50 26 300000 Senior Developer
Raghav CIR28 25 280000 HR
Dropping a range of columns from 'Age' to 'Role' using iloc() function
Employee ID
Nimesh CIR45
Arjun CIR12
Mohan CIR18
Ritesh CIR50
Raghav CIR28
使用 loc() 函式
如果我們想使用標籤而不是索引來建立範圍,則可以使用“**loc() 函式**”。
示例
我們藉助“**loc()**”函式建立了一個範圍。與 iloc() 不同,它包含最後一列。“loc()”函式透過將列名作為引數來選擇列。最後,我們列印了包含剩餘列的新 DataFrame。
colDrop = dataframe.drop(dataframe.loc[:, "Age": "Role"].columns, axis=1)
print("Dropping a range of columns from Age to Role using loc() fucntion")
print(colDrop)
輸出
Employee ID Age Salary Role
Nimesh CIR45 25 200000 Junior Developer
Arjun CIR12 28 250000 Analyst
Mohan CIR18 27 180000 Programmer
Ritesh CIR50 26 300000 Senior Developer
Raghav CIR28 25 280000 HR
Dropping a range of columns from Age to Role using loc() fucntion
Employee ID
Nimesh CIR45
Arjun CIR12
Mohan CIR18
Ritesh CIR50
Raghav CIR28
結論
本文重點介紹了從 Pandas DataFrame 中刪除列的簡單操作。我們討論了兩種技術,即“**按標籤名稱刪除**”和“**按索引值刪除**”。我們還使用了“loc()”和“iloc()”函式,並確認了它們在 Pandas DataFrame 上的應用。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP