【发布时间】:2021-08-21 00:05:36
【问题描述】:
我将数据存储在 NumPy 结构化数组中,其中部分信息标识了各种情况。我想找到与给定案例匹配的 row。例如,假设我将建筑物的名称、房间号以及房间中的椅子和桌子的数量存储在 (2,) 数组中。然后看起来像这样:
import numpy as np
my_dtype = [('building', '<U5'), ('room', '<i8'), ('seating', '<i8', (2,))]
room_info = np.array([('BLDG0', 12, [24, 6]),
('BLDG1', 34, [32, 10]),
('BLDG0', 14, [10, 20])],
dtype=my_dtype)
现在说我想找到建筑物'BLDG0',房间14的行。根据Finding a matching row in a numpy matrix的回答,我试过了
sub_fields = ['building', 'room']
matching_index, = np.where(room_info[sub_fields] == ('BLDG0', 14))
理想情况下会产生[2]。但是,这会导致以下警告:
FutureWarning: elementwise == comparison failed and returning scalar instead; this will raise an error or perform elementwise comparison in the future.
并返回一个空数组。除了分别比较每个列然后找到匹配的索引之外,有没有办法为大量数据找到匹配的子行?
我正在通过 miniconda 使用 NumPy 版本 1.18.5,看起来我无法在此环境中安全地更新到更新版本。 (虽然我不确定新版本是否支持这种类型的比较)
【问题讨论】:
标签: python numpy structured-array