熊猫分发
1. Pandas drop()函数语法 (1. Pandas drop() Function Syntax)
Pandas DataFrame drop() function allows us to delete columns and rows. The drop() function syntax is:
Pandas DataFrame drop()函数允许我们删除列和行。 drop()函数的语法为:
drop(
self,
labels=None,
axis=0,
index=None,
columns=None,
level=None,
inplace=False,
errors="raise"
)
- labels: The labels to remove from the DataFrame. It’s used with ‘axis’ to identify rows or column names. 标签 :要从DataFrame中删除的标签。 它与“轴”一起使用以标识行或列的名称。
- axis: The possible values are {0 or ‘index’, 1 or ‘columns’}, default 0. It’s used with ‘labels’ to specify rows or columns. axis :可能的值为{0或'index',1或'columns'},默认值为0。它与'labels'一起使用以指定行或列。
- index: indexes to drop from the DataFrame. index :要从DataFrame中删除的索引。
- columns: columns to drop from the DataFrame. columns :要从DataFrame中删除的列。
- level: used to specify the level incase of MultiIndex DataFrame. level :用于指定MultiIndex DataFrame的级别。
- inplace: if True, the source DataFrame is changed and None is returned. The default value is False, the source DataFrame remains unchanged and a new DataFrame object is returned. inplace :如果为True,则更改源DataFrame并返回None。 默认值为False,源DataFrame保持不变,并返回一个新的DataFrame对象。
- errors: the possible values are {‘ignore’, ‘raise’}, default ‘raise’. If the DataFrame doesn’t have the specified label, KeyError is raised. If we specify errors as ‘ignore’, the error is suppressed and only existing labels are removed. 错误 :可能的值为{'ignore','raise'},默认为'raise'。 如果DataFrame没有指定的标签,则会引发KeyError。 如果我们将错误指定为“忽略”,则错误将被抑制并且仅现有标签被删除。
Let’s look into some of the examples of using the Pandas DataFrame drop() function.
让我们看一些使用Pandas DataFrame drop()函数的示例。
2.熊猫掉落柱 (2. Pandas Drop Columns)
We can drop a single column as well as multiple columns from the DataFrame.
我们可以从DataFrame中删除单个列以及多个列。
2.1)删除单列 (2.1) Drop Single Column)
import pandas as pd
d1 = {'Name': ['Pankaj', 'Meghna', 'David'], 'ID': [1, 2, 3], 'Role': ['CEO', 'CTO', 'Editor']}
source_df = pd.DataFrame(d1)
print(source_df)
# drop single column
result_df = source_df.drop(columns='ID')
print(result_df)
Output:
输出:
Name ID Role
0 Pankaj 1 CEO
1 Meghna 2 CTO
2 David 3 Editor
Name Role
0 Pankaj CEO
1 Meghna CTO
2 David Editor
2.2)删除多列 (2.2) Drop Multiple Columns)
result_df = source_df.drop(columns=['ID', 'Role'])
print(result_df)
Output:
输出:
Name
0 Pankaj
1 Meghna
2 David
3.熊猫落行 (3. Pandas Drop Rows)
Let’s look into some examples to drop a single row and multiple rows from the DataFrame object.
让我们看一些示例,从DataFrame对象中删除单行和多行。
3.1)删除单行 (3.1) Drop Single Row)
import pandas as pd
d1 = {'Name': ['Pankaj', 'Meghna', 'David'], 'ID': [1, 2, 3], 'Role': ['CEO', 'CTO', 'Editor']}
source_df = pd.DataFrame(d1)
result_df = source_df.drop(index=0)
print(result_df)
Output:
输出:
Name ID Role
1 Meghna 2 CTO
2 David 3 Editor
3.2)删除多行 (3.2) Drop Multiple Rows)
result_df = source_df.drop(index=[1, 2])
print(result_df)
Output:
输出:
Name ID Role
0 Pankaj 1 CEO
4.将DataFrame的列和行放在适当的位置 (4. Drop DataFrame Columns and Rows in place)
We can specify inplace=True
to drop columns and rows from the source DataFrame itself. In this case, None is returned from the drop() function call.
我们可以指定inplace=True
从源DataFrame本身删除列和行。 在这种情况下,drop()函数调用将返回None。
import pandas as pd
d1 = {'Name': ['Pankaj', 'Meghna', 'David'], 'ID': [1, 2, 3], 'Role': ['CEO', 'CTO', 'Editor']}
source_df = pd.DataFrame(d1)
source_df.drop(columns=['ID'], index=[0], inplace=True)
print(source_df)
Output:
输出:
Name Role
1 Meghna CTO
2 David Editor
5.使用标签和轴放置列和行 (5. Using labels and axis to drop columns and rows)
It’s not the recommended approach to delete rows and columns. But, it’s good to know because the ‘index’ and ‘columns’ parameters were introduced to drop() function in pandas version 0.21.0. So you may encounter it for older code.
不建议删除行和列。 但是,很高兴知道这一点,因为在熊猫0.20.2版中,drop()函数引入了'index'和'columns'参数。 因此,对于较旧的代码,您可能会遇到它。
import pandas as pd
d1 = {'Name': ['Pankaj', 'Meghna', 'David'], 'ID': [1, 2, 3], 'Role': ['CEO', 'CTO', 'Editor']}
source_df = pd.DataFrame(d1)
# drop rows
result_df = source_df.drop(labels=[0, 1], axis=0)
print(result_df)
# drop columns
result_df = source_df.drop(labels=['ID', 'Role'], axis=1)
print(result_df)
Output:
输出:
Name ID Role
2 David 3 Editor
Name
0 Pankaj
1 Meghna
2 David
6.抑制删除列和行中的错误 (6. Suppressing Errors in Dropping Columns and Rows)
If the DataFrame doesn’t contain the given labels, KeyError is raised.
如果DataFrame不包含给定的标签,则会引发KeyError。
result_df = source_df.drop(columns=['XYZ'])
Output:
输出:
KeyError: "['XYZ'] not found in axis"
We can suppress this error by specifying errors='ignore'
in the drop() function call.
我们可以通过在drop()函数调用中指定errors='ignore'
来抑制此错误。
result_df = source_df.drop(columns=['XYZ'], errors='ignore')
print(result_df)
Output:
输出:
Name ID Role
0 Pankaj 1 CEO
1 Meghna 2 CTO
2 David 3 Editor
7.结论 (7. Conclusion)
Pandas DataFrame drop() is a very useful function to drop unwanted columns and rows. There are two more functions that extends the drop() functionality.
Pandas DataFrame drop()是删除不需要的列和行的非常有用的函数。 还有两个函数扩展了drop()功能。
- drop_duplicates() to remove duplicate rows drop_duplicates()删除重复的行
- dropna() to remove rows and columns with missing values dropna()删除缺少值的行和列
8.参考 (8. References)
翻译自: https://www.journaldev.com/33484/pandas-drop-columns-rows
熊猫分发