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

| |
[ 所属分类 开发（python） | 发布者 店小二03 | 时间 | 作者 红领巾 ] 0人收藏点击收藏

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]])

tags: gt,array,axis,numpy,obj,arr,dataset,np,None,一列

1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责；
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性，不作出任何保证或承若；
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。