我试图通过我收集的另一个数组(具有相同的值)过滤我的ndarray
我的主要ndarray看起来像
[['Name' 'Col1' 'Count']
['test' '' '413']
['erd' ' ' '60']
...,
['Td1' 'f' '904']
['Td2' 'K' '953']
['Td3' 'r' '111']]
我有另一个列表,其中包含各种匹配的名称
names = ['Td1','test','erd']
我想做什么
我想使用列表名称作为针对上面的ndarray的过滤器吗?
我尝试过什么
name_filter = main_ndarray[:,0] == names
这不起作用
我期待的是
[['Name' 'Col1' 'Count']
['test' '' '413']
['erd' ' ' '60']
['Td1' 'f' '904']]
答案 0 :(得分:1)
您也可以使用filter
功能。
cats_array = numpy.array(
[['Name' ,'Col1', 'Count'],
['test', '' ,'413'],
['erd' ,' ' ,'60'],
['Td1' ,'f' ,'904'],
['Td2' ,'K' ,'953'],
['Td3' ,'r', '111']]
)
names = ['Td1','test','erd']
filter(lambda x: x[0] in names, cats_array)
给出:
[array(['test', '', '413'],
dtype='|S5'), array(['erd', ' ', '60'],
dtype='|S5'), array(['Td1', 'f', '904'],
dtype='|S5')]
答案 1 :(得分:1)
考虑将Pandas用于此类数据:
import pandas as pd
data = [['Name', 'Col1', 'Count'],
['test', '', '413'],
['erd', ' ', '60'],
['Td1', 'f', '904'],
['Td2', 'K', '953'],
['Td3', 'r', '111']]
df = pd.DataFrame(data[1:], columns=data[0])
names = ['Td1','test','erd']
result = df[df.Name.isin(names)]
结果:
>>> df
Name Col1 Count
0 test 413
1 erd 60
2 Td1 f 904
3 Td2 K 953
4 Td3 r 111
>>> result
Name Col1 Count
0 test 413
1 erd 60
2 Td1 f 904
>>>
参考
答案 2 :(得分:1)
我也会选择@ YXD的Pandas解决方案,但为了完整起见,我还提供了一个基于列表理解的简单解决方案:
data = [['Name', 'Col1', 'Count'],
['test', '', '413'],
['erd', ' ', '60'],
['Td1', 'f', '904'],
['Td2', 'K', '953'],
['Td3', 'r', '111']]
names = ['Td1', 'test', 'erd']
# select all sublist of data
res = [l for l in data if l[0] in names]
# insert the first row of data
res.insert(0, data[0])
然后为您提供所需的输出:
[['Name', 'Col1', 'Count'],
['test', '', '413'],
['erd', ' ', '60'],
['Td1', 'f', '904']]