python删除数组的某一行_如何使用条件从numpy数组中删除特定行?

This is the code

a = np.array([[ 0, 1],

[ 3, 11],

[4,2]])

This is what I tried

a= a[a[0]>0,:]

It works fine when I only have two elements, but anything more it throws an error.What I am trying to do is that in the first column if there's a value less than one than I need to delete that entire row.

so the expected output is

([ 3, 11],

[4,2]])

I was hoping for a solution which I could generalize even if there were more than 2 elements per item such as

([2,3,4,5],

[8,2,4,6],

[2,4,9,1],

[5,3,2,0],)

then the application of the code will give a result such as

([2,3,4,5],

[8,2,4,6],

[2,4,9,1],)

Any suggestions.

解决方案

For just the first column use a[:,0] > 0 which will pull all the values from the first column and check which are > 0 or whatever condition you want:

In [50]: a = np.array([[ 0, 1],

[ 3, 11],

[4,2]])

In [51]: a[a[:,0] > 0]

Out[51]:

array([[ 3, 11],

[ 4, 2]])

You can use all if you want to check all values in each row:

In [43]: a = np.array([[ 0, 1],

[ 3, 11],

[4,2]])

In [44]: a[(a >= 0).all(axis=1)]

Out[44]:

array([[ 3, 11],

[ 4, 2]])

In [45]: a = np.array ([[2,3,4,5],

[8,2,4,6],

[2,4,9,1],

[5,3,2,0]])

In [46]: a[(a > 0).all(axis=1)]

Out[46]:

array([[2, 3, 4, 5],

[8, 2, 4, 6],

[2, 4, 9, 1]])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值