numpy.delete删除一列或多列的方法

yipeiwu_com5年前Python基础

基础介绍:

numpy.delete
numpy.delete(arr, obj, axis=None)[source]
 Return a new array with sub-arrays along an axis deleted. For a one dimensional array, this returns those entries not returned by arr[obj].
 Parameters:	
 arr : array_like
  Input array.
 obj : slice, int or array of ints
  Indicate which sub-arrays to remove.
 axis : int, optional
  The axis along which to delete the subarray defined by obj. If axis is None, obj is applied to the flattened array.
 Returns:	
 out : ndarray
  A copy of arr with the elements specified by obj removed. Note that delete does not occur in-place. If axis is None, out is a flattened array.

示例:

1.删除一列

>>> dataset=[[1,2,3],[2,3,4],[4,5,6]] 
>>> import numpy as np 
>>> dataset = np.delete(dataset, -1, axis=1) 
>>> dataset 
array([[1, 2], 
  [2, 3], 
  [4, 5]]) 

2.删除多列

arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) 
np.delete(arr, [1,2], axis=1) 
array([[ 1, 4], 
  [ 5, 8], 
  [ 9, 12]]) 

以上这篇numpy.delete删除一列或多列的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

用Python实现大文本文件切割的方法

在实际工作中,有些场景下,因为产品既有功能限制,不支持特大文件的直接处理,需要把大文件进行切割处理。 当然可以通过UltraEdit编辑工具,或者从网上下载一些文件切割器之类的。但这些要...

Python 多线程不加锁分块读取文件的方法

Python 多线程不加锁分块读取文件的方法

多线程读取或写入,一般会涉及到同步的问题,否则产生的结果是无法预期的。那么在读取一个文件的时候,我们可以通过加锁,但读不像写操作,会导致文件错误,另外锁操作是有一定的耗时。因此通过文件分...

Python中第三方库Requests库的高级用法详解

一、Requests库的安装 利用 pip 安装,如果你安装了pip包(一款Python包管理工具,不知道可以百度哟),或者集成环境,比如Python(x,y)或者anaconda的话...

在Python中Dataframe通过print输出多行时显示省略号的实例

在Python中Dataframe通过print输出多行时显示省略号的实例

笔者使用Python进行数据分析时,通过print输出Dataframe中的数据,当Dataframe行数很多时,中间部分显示省略号,如下图所示: 0 项华祥 1...

Python使用base64模块进行二进制数据编码详解

前言 昨天团队的学妹来问关于POP3协议的问题,所以今天稍稍研究了下POP3协议的格式和Python里面的poplib。而POP服务器往回传的数据里有一部分需要用到Base64进行解码,...