过滤和在一个numpy的阵列求和不同的列 [英] Filter and sum different columns in a numpy array

查看:385
本文介绍了过滤和在一个numpy的阵列求和不同的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大numpy的阵列数据,我希望通过一列来过滤 [:, 8]< =半径,并获得不同的列的总和 [:, 7]

I have a large numpy array data that I wish to filter by one column [:,8] <= radius and get the sum of a different column [:,7]

到目前为止,我有它返回一个无效片的错误以下。

So far I have the following which returns an "invalid slice" error.

>>> data.slice
(4700, 9)

>>> np.sum(data[np.where(data[:,8] <= 50):,7])
IndexError: invalid slice

我是pretty新的Python所以真的似乎无法找出我在做什么错在这里。任何想法或解释将AP preciated。

I'm pretty new to python so really can't seem to figure out what I'm doing wrong here. Any thoughts or explanations would be appreciated.

推荐答案

有没有必要为 np.where 电话。

data = np.random.normal(size=(20, 2))
np.sum(data[data[:,0] < 0, 1])

在这个例子中,我要行,其中数据[:0] LT; 0 ,我想列 1 。所以只要这些切片和采取的总和。

In this example, I want the rows where data[:,0] < 0 is True, and I want column 1. So just slice with those and take the sum.

这篇关于过滤和在一个numpy的阵列求和不同的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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