如何删除 numpy.array 中的列 [英] How to delete columns in numpy.array

查看:26
本文介绍了如何删除 numpy.array 中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除 numpy.array 中的选定列.这就是我所做的:

I would like to delete selected columns in a numpy.array . This is what I do:

n [397]: a = array([[ NaN,   2.,   3., NaN],
   .....:        [  1.,   2.,   3., 9]])

In [398]: print a
[[ NaN   2.   3.  NaN]
 [  1.   2.   3.   9.]]

In [399]: z = any(isnan(a), axis=0)

In [400]: print z
[ True False False  True]

In [401]: delete(a, z, axis = 1)
Out[401]:
 array([[  3.,  NaN],
       [  3.,   9.]])

在这个例子中,我的目标是删除所有包含 NaN 的列.我期待最后一个命令导致:

In this example my goal is to delete all the columns that contain NaN's. I expect the last command to result in:

array([[2., 3.],
       [2., 3.]])

我该怎么做?

推荐答案

顾名思义,我认为标准的方式应该是delete:

Given its name, I think the standard way should be delete:

import numpy as np

A = np.delete(A, 1, 0)  # delete second row of A
B = np.delete(B, 2, 0)  # delete third row of B
C = np.delete(C, 1, 1)  # delete second column of C

根据 numpy 的文档页面numpy.delete的参数如下:

numpy.delete(arr, obj, axis=None)

  • arr 指的是输入数组,
  • obj 指的是哪些子数组(例如列/行号或数组的切片)和
  • axis 是指按列(axis = 1)或按行(axis = 0)删除操作.
  • arr refers to the input array,
  • obj refers to which sub-arrays (e.g. column/row no. or slice of the array) and
  • axis refers to either column wise (axis = 1) or row-wise (axis = 0) delete operation.

这篇关于如何删除 numpy.array 中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆