熊猫分发

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 pdd1 = {'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  EditorName    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 pdd1 = {'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 pdd1 = {'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 pdd1 = {'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  EditorName
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()功能。

  1. drop_duplicates() to remove duplicate rowsdrop_duplicates()删除重复的行
  2. dropna() to remove rows and columns with missing valuesdropna()删除缺少值的行和列

8.参考 (8. References)

  • Python Pandas Module TutorialPython Pandas模块教程
  • pandas drop() API Doc熊猫drop()API文档

翻译自: https://www.journaldev.com/33484/pandas-drop-columns-rows

熊猫分发

熊猫分发_熊猫下降列和行相关推荐

  1. 熊猫分发_熊猫新手:第一部分

    熊猫分发 For those just starting out in data science, the Python programming language is a pre-requisite ...

  2. 熊猫分发_熊猫cut()函数示例

    熊猫分发 1.熊猫cut()函数 (1. Pandas cut() Function) Pandas cut() function is used to segregate array element ...

  3. 熊猫分发_熊猫重命名列和索引

    熊猫分发 Sometimes we want to rename columns and indexes in the Pandas DataFrame object. We can use pand ...

  4. 熊猫分发_熊猫新手:第二部分

    熊猫分发 This article is a continuation of a previous article which kick-started the journey to learning ...

  5. 熊猫分发_熊猫实用指南

    熊猫分发 什么是熊猫? (What is Pandas?) Pandas is an open-source data analysis and manipulation tool for Pytho ...

  6. 熊猫分发_与熊猫度假

    熊猫分发 While working on a project recently, I had to work with time series data spread over a year. I ...

  7. 熊猫数据集_熊猫迈向数据科学的第一步

    熊猫数据集 I started learning Data Science like everyone else by creating my first model using some machi ...

  8. 熊猫分发_实用熊猫指南

    熊猫分发 Pandas is a very powerful and versatile Python data analysis library that expedites the data an ...

  9. 熊猫分发_流利的熊猫

    熊猫分发 Let's uncover the practical details of Pandas' Series, DataFrame, and Panel 让我们揭露Pandas系列,DataF ...

最新文章

  1. 第八届全球游戏大会(GMGC北京2019)
  2. 详解优酷视频质量评价体系
  3. 7-234 两个有序序列的中位数 (25 分)
  4. Linux tcpdump
  5. 华为 P40 “一胞三胎”,最贵价 10854 元
  6. SharePoint创建登录表单
  7. mysql怎样在bat脚本中添加日志_如何在windows下用bat脚本定时备份mysql
  8. ExtJS用户带验证码登录页面
  9. php如何在sql语句中使用,php – 如何在SQL查询中使用数组
  10. tx2开发板接口详解_Linux CAN编程详解
  11. 两强格局初定,网易云能拿什么跟腾讯音乐打
  12. 双活数据中心存储问题梳理
  13. Ubuntu的shell脚本踩keng-unexpected operator
  14. 5G差异化业务场景需求及网络切片(1)
  15. 两个网段共享打印机_不同ip段共享打印机设置方法
  16. 2015校招季,阿里、搜狗、百度、蘑菇街面试总结
  17. 关于Java对接读卡器遇到的坑Process finished with exit code -1073740940 (0xC0000374)
  18. 8bit校准测试工具mtd-utils的移植和使用
  19. Window server 2008 搭建FTP服务器
  20. 兔将十年大作《赤狐书生》特效解析:青蛙精篇

热门文章

  1. 实现iOS长时间后台的两种方法:Audiosession和VOIP(转)
  2. 注意地方hadoop中的pi值计算
  3. 搜狗云输入法对外提供调用体验
  4. Asp.net中Global.asax
  5. Linux编程获取本机IP地址
  6. Java调用Javascript、Python算法总结
  7. MyEclipse的html页面 design视图中 关闭可视化界面
  8. 我们为什么需要SDN?---致新人
  9. django 数据库交互2
  10. [转]Time Tracker Starter Kit 简介