按标签名称或索引位置删除 DataFrame 中的列
Pandas 数据框是一种由一系列实体组成的 2D 数据结构。它在数学数据分析中非常有用。数据以表格形式排列,每行都作为数据的一个实例。
Pandas 数据框很特别,因为它具有众多功能,使其成为非常强大的编程资产。数据框中的每一列代表一系列带标签的信息。在本文中,我们将对这些列进行操作,并讨论在 Pandas 数据框中删除列的各种方法。
可以通过指定列名或借助其索引值来删除单个或多个列。我们将了解这两种方法,但首先我们必须准备一个数据集并生成一个数据框。
创建数据框
创建数据框时,我们可以为表分配列名和行名。此过程很重要,因为它指定了"标签名称"和"索引值"。
在这里,我们将 pandas 库导入为"pd",然后使用列表字典传递数据集。每个键代表一个列数据,与其关联的值以列表的形式传递。我们使用 pandas"DataFrame()"函数创建了数据框。我们在"index"参数的帮助下将行标签分配给数据框。现在让我们使用列名删除列。
示例
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.drop"方法从数据框中删除"Salary"和"Role"列。我们将这些列名传递到一个列表中。
我们将"axis"值指定为 1,因为我们在列轴上操作。最后,我们将这个新的数据框存储在变量"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
从数据框中删除一系列列
在上面讨论的示例中,我们仅删除了特定列(Salary 和 Role),但众所周知,pandas 为程序员提供了许多便利,因此我们可以使用它来创建要删除的一系列列。让我们实现这个逻辑。
使用 iloc() 函数
生成数据框后,我们使用"iloc() 函数"选择一系列列并将其从数据框中删除。"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()"函数通过将列名作为参数来选择列。最后,我们用剩余的列打印了新的数据框。
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 数据框中删除列的简单操作。我们讨论了两种技术,即"按标签名称删除"和"按索引值删除"。我们还使用了"loc()"和"iloc()"函数,并确认了它们在 pandas 数据框中的应用。