通过标签名称或索引位置删除 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