drop函数

DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

这是drop函数的所有参数

  • labels是指要删除的标签,一个或者是列表形式的多个;
  • axis是指处哪一个轴;
  • columns是指某一列或者多列;
  • level是指等级,针对多重索引的情况;
  • inplaces是否替换原来的dataframe;

具体更详细的可以参阅官网:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html

Axis(轴)含义

axis=0指的是逐行,axis=1指的是逐列。

>>> import pandas as pd
>>> df = pd.DataFrame([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], columns=["col1", "col2", "col3", "col4"])
>>> print(df.mean(axis=0))
col1 2.0
col2 2.0
col3 2.0
col4 2.0
dtype: float64
>>> print(df.mean(axis=1))
0 1.0
1 2.0
2 3.0
dtype: float64
>>> print(df.drop(0,axis=0))
col1 col2 col3 col4
1 2 2 2 2
2 3 3 3 3
>>> print(df.drop(['col1'],axis=1))
col2 col3 col4
0 1 1 1
1 2 2 2
2 3 3 3

根据结果:

mean(axis=0)计算的是每一列平均值,
mean(axis=1)计算的是每一行平均值。
drop(0,axis=0)删除行,
drop([‘col1’],axis=1)删除列。

drop用法实验

>>> df = pd.DataFrame(np.arange(12).reshape(3,4),
... columns=['A', 'B', 'C', 'D'])
>>> df
A B C D
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
#指定删除相关的列,没有带columns,所以要指出是哪个轴上的
>>> df.drop(['B', 'C'], axis=1)
A D
0 0 3
1 4 7
2 8 11
#这里带有columns,所以不用加上axis参数
>>> df.drop(columns=['B', 'C'])
A D
0 0 3
1 4 7
2 8 11 #删除指定索引的行,这里没有axis参数,就是默认axis=0,也就是删除行
>>> df.drop([0, 1])
A B C D
2 8 9 10 11 #多重索引的情况,因为版本问题,有些版本需要把里面的codes改成labels
>>> midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
... ['speed', 'weight', 'length']],
... codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
>>> df = pd.DataFrame(index=midx, columns=['big', 'small'],
... data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
... [250, 150], [1.5, 0.8], [320, 250],
... [1, 0.8], [0.3,0.2]])
>>> df
big small
lama speed 45.0 30.0
weight 200.0 100.0
length 1.5 1.0
cow speed 30.0 20.0
weight 250.0 150.0
length 1.5 0.8
falcon speed 320.0 250.0
weight 1.0 0.8
length 0.3 0.2 >>> df.drop(index='cow', columns='small')
big
lama speed 45.0
weight 200.0
length 1.5
falcon speed 320.0
weight 1.0
length 0.3 >>> df.drop(index='length', level=1)
big small
lama speed 45.0 30.0
weight 200.0 100.0
cow speed 30.0 20.0
weight 250.0 150.0
falcon speed 320.0 250.0
weight 1.0 0.8 #我这里不加index参数是因为我的版本加上以后会报错,所以在使用时建议先了解一下版本
df.drop('length', level=0) big small
lama speed 45.0 30.0
weight 200.0 100.0
length 1.5 1.0
cow speed 30.0 20.0
weight 250.0 150.0
length 1.5 0.8
falcon speed 320.0 250.0
weight 1.0 0.8
length 0.3 0.2

delete函数

具体的用法如下:

del df['A']  # 删除A列,会就地修改

另外,可能drop函数相关的函数还有一些dropna()和drop_duplicated()函数,暂不总结了

[Python] Pandas的delete、drop函数的用法的更多相关文章

  1. python学习笔记之open函数的用法

    先上一段代码 >>> f = open('1.txt','r'); >>> f.readline() #读取数据>>> f.close() #关闭 ...

  2. python pandas stack和unstack函数

    在用pandas进行数据重排时,经常用到stack和unstack两个函数.stack的意思是堆叠,堆积,unstack即"不要堆叠",我对两个函数是这样理解和区分的. 常见的数据 ...

  3. python中range()、list()函数的用法

      Python  range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表. 函数语法: range(stop) range(start, stop , ...

  4. python中split()函数的用法

    函数:split() Python中有split()和os.path.split()两个函数,具体作用如下:split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(lis ...

  5. python中字符串中一些函数的用法

    1..capitalize():字符串的首字母大写: 2..count():字符串中的某个字母的个数: 3..center(50,'-'):对象居中,且左右用'-'补齐: 4..encode():吧字 ...

  6. 关于python中的operator.itemgetter()函数的用法

    1. operator.itemgetter(num)函数 表示对对象的第num维数据进行操作获取. >>>import operator >>>a = [1, 2 ...

  7. python中join()函数的用法

    join()函数 语法:  'sep'.join(s) 参数说明 sep:分隔符.可以为空 s:要连接的元素序列.字符串.元组.字典 上面的语法即:以sep作为分隔符,将s所有的元素合并成一个新的字符 ...

  8. [Python] Pandas 中 Series 和 DataFrame 的用法笔记

    目录 1. Series对象 自定义元素的行标签 使用Series对象定义基于字典创建数据结构 2. DataFrame对象 自定义行标签和列标签 使用DataFrame对象可以基于字典创建数据结构 ...

  9. python进行数据清理之pandas中的drop用法

    好久好久没有更新博客了,之前自学的估计也都忘记差不多了.由于毕业选择从事的行业与自己的兴趣爱好完全两条路,心情也难过了很久,既然入职了就要好好干,仍要保持自己的兴趣,利用业余时间重拾之前的乐趣. 从基 ...

随机推荐

  1. Sqli - Labs 靶场笔记(一)

    Less - 1: 页面: URL: http://127.0.0.1/sqli-labs-master/Less-1/ 测试: 1.回显正常,说明不是数字型注入, http://127.0.0.1/ ...

  2. WPF NET5 Prism8.0的升级指南

    前言 ​ 曾经我以学习的目的写了关于在.NET Core3.1使用Prism的系列文章.NET Core 3 WPF MVVM框架 Prism系列文章索引,也谢谢大家的支持,事实上当初的版本则是Pri ...

  3. Docker-常用基建的安装与部署

    一:Docker安装 1:通过yum安装docker yum -y install gcc yum -y install gcc-c++ yum remove docker \ docker-clie ...

  4. Eclipse中的可视化图形界面设计插件windowbuilder

    对于eclipse平台上的可视化开发工具插件,有windowbuilder.visual editor等,今天就对windowbuilder说明: WindowBuilder功能特性等介绍,参考如下网 ...

  5. java之 Request

    0x01.Request 什么是request 在Servlet API中,定义了一个HttpServletRequest接口,它继承自ServletRequest接口,专门用来封装HTTP请求消息. ...

  6. shell命令分隔符 二叉树结构的命令行树

    shell命令分隔符 二叉树结构的命令行树 I  ;&

  7. token的分层图如下

    基于 token 的多平台身份认证架构设

  8. BigDecimal add方法问题:调用add后,求和结果没变

    import java.math.BigDecimal; public class DecimalAdd { public static void main(String[] args) { BigD ...

  9. 一个sql盲注小工具 (Golang版)

    并发,二分法判断. 源码写的有点垃圾,有点乱,结果也存在一些缺失. 记录: sql:select distinct 中的distinct选项,这是只会获取你表中不重复数据,是表中,而不是你一次sql执 ...

  10. 引入 Gateway 网关,这些坑一定要学会避开!!!

    Spring cloud gateway是替代zuul的网关产品,基于Spring 5.Spring boot 2.0以上.Reactor, 提供任意的路由匹配和断言.过滤功能.上一篇文章谈了一下Ga ...