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. 【Oracle】转:通过案例学调优之--Oracle Time Model(时间模型)

    转自:http://blog.51cto.com/tiany/1596012 通过案例学调优之--Oracle Time Model(时间模型) 数据库时间 优化不仅仅是缩短等待时间.优化旨在缩短最终 ...

  2. 前端面试之ES6新增了数组中的的哪些方法?!

    前端面试之ES6新增了数组中的的哪些方法?! 我们先来看看数组中以前有哪些常用的方法吧! 1 新增的方法! 1 forEach() 迭代遍历数组 回调函数中的三个参数 value: 数组中的每一个元素 ...

  3. C#高级编程第11版 - 第二章 索引

    [1]2.1.1 Hello,World! 1. using static System.Console; // ... WriteLine("Hello World!"); 提前 ...

  4. 关于BI测试

    BI测试: BI是从数据接入.数据准备.数据分析.数据可视化到数bai据分发应用的一系列过程,目的是为了辅助企业高效决策.而报表虽然最终也实现了数据可视化,但是对于数据分析的维度.深度.颗粒度.实时性 ...

  5. 13 | 实战:单机如何实现管理百万主机的心跳服务? https://time.geekbang.org/column/article/240656

    13 | 实战:单机如何实现管理百万主机的心跳服务? https://time.geekbang.org/column/article/240656

  6. Python学习【第3篇】:列表魔法

    ##########################深灰魔法-list类中提供的方法###################list 类,列表list = [1,12,9,"age" ...

  7. codevs3639

    题目描述 Description 给出一棵树,求出树的中心. 为了定义树的中心,首先给每个结点进行标号.对于一个结点K,如果把K从树中删除(连同与它相连的边一起),剩下的被分成了很多块,每一块显然又是 ...

  8. Excel 一张表最多能装下多少行多少列数据?

    一个工作簿可以装下255张,那么每张工作表可以装下多少行多少列数据呢? 1.任意打开或新建一个Excel文档. 2.在文档中,找到其左上角的"文件"按钮,点击选择"选项& ...

  9. boss导出简历css

    $('body').css('background-color', '#fff')$('.keywords').hide()$('#wrap').html($('.resume-box').css(' ...

  10. C链表-C语言入门经典例题

    struct student { long num; float score; struct student *next; }; 注意:只是定义了一个struct student类型,并未实际分配存储 ...