如何从numpy数组中提取行,其中col1 = a

问题描述

我每年有二维温度值数组,其中每年有一些温度值。所以数组看起来像这样:

[[1960,a,b,c,d,e,f ...],[1960,a1,b1,c1,d1,e1,f1 ...],a2,b2,c2,d2,e2,f2 ...],[1961,[1962,f2 ...]....]

我需要为第0列为1961的行提取第1列中的所有内容(因此取所有a值)。

有人知道我会怎么做吗? 谢谢!

解决方法

让L成为您的数组。然后使用以下代码。

L[L[:,1] == 1960][:,0] 
,

适用于常规python列表的解决方案:

[item[1] for item in array if item[0]==1961]

其中array是您的2d输入数组。

,

使用切片
选择第二个值

a[a[:,0] == '1961',1]

或所有值

a[a[:,0] == '1961']
,
import pandas as pd
raw_data = [[1960,'a','b','c','d','e','f'],[1960,'a1','b1','c1','d1','e1','f1'],'a2','b2','c2','d2','e2','f2'],[1961,'f2']]

data = pd.DataFrame(raw_data)
print(data)
      0   1   2   3   4   5   6
0  1960   a   b   c   d   e   f
1  1960  a1  b1  c1  d1  e1  f1
2  1960  a2  b2  c2  d2  e2  f2
3  1961   a   b   c   d   e   f
4  1961  a1  b1  c1  d1  e1  f1
5  1961  a2  b2  c2  d2  e2  f2
print(data.loc[data.iloc[:,0] == 1961,1]) 
3     a
4    a1
5    a2
Name: 1,dtype: object

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...