相当于 pandas 的 numpy

numpy equivalent of pandas

我有以下 numpy 数组:

x = np.arange(9.).reshape(3, 3)

因此:

>>>> x
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.],
       [ 6.,  7.,  8.]])

我想select第三列大于2的所有行。

在pandas,我会做:

import pandas as pd
d = DataFrame(x)

>>>> d[d.iloc[:,2]>2]

输出为:

   0  1  2
1  3  4  5
2  6  7  8

如何使用 numpy 获得相同的输出?我试过 numpy where 但它 returns 索引,而不是值。 谢谢!

In [120]:
x[x[: , 2] > 2]
Out[120]:
array([[ 3.,  4.,  5.],
       [ 6.,  7.,  8.]])

分解


In [122]:
mask = x[: , 2] > 2
mask
Out[122]:
array([False,  True,  True], dtype=bool)

In [123]:
x[mask]
Out[123]:
array([[ 3.,  4.,  5.],
       [ 6.,  7.,  8.]])