Ale*_*ich 3 python numpy subset pandas
我有以下numpy数组:
x = np.arange(9.).reshape(3, 3)
Run Code Online (Sandbox Code Playgroud)
从而:
>>>> x
array([[ 0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.]])
Run Code Online (Sandbox Code Playgroud)
我想选择第三列大于2的所有行.
在熊猫中,我会这样做:
import pandas as pd
d = DataFrame(x)
>>>> d[d.iloc[:,2]>2]
Run Code Online (Sandbox Code Playgroud)
输出将是:
0 1 2
1 3 4 5
2 6 7 8
Run Code Online (Sandbox Code Playgroud)
如何使用numpy获得相同的输出?我尝试了numpy,但它返回索引,而不是值.谢谢!
In [120]:
x[x[: , 2] > 2]
Out[120]:
array([[ 3., 4., 5.],
[ 6., 7., 8.]])
Run Code Online (Sandbox Code Playgroud)
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.]])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
727 次 |
| 最近记录: |