As with index arrays, what is returned is a copy of the data, not a view as one gets with slices.Unlike some of the references (such as array and mask indices) assignments are always made to the original data in the array (indeed, nothing else would make sense!).
但你不能在这里这么做。虽然您可以指定给sdd[sdd['name'] == 'song']并修改原始数组,但分配给sdd[sdd['name'] == 'song']['age']就是修改副本。在
要理解原因,您必须了解Python中索引的工作原理:像spam[eggs][beans][toast] = value这样的目标调用spam.__getitem__(eggs).__getitem__(beans).__setitem__(toast, value);NumPy无法分辨出sdd[sdd['name'] == 'song']部分是被查找来分配某个东西而不是访问它的。在
文件解释了NumPy如何处理这个简单的案例:… a new array is extracted from the original (as a temporary) containing the values at 1, 1, 3, 1, then the value 1 is added to the temporary, and then the temporary is assigned back to the original array.
因此,如果有多个[…]索引,则必须手动执行相同的操作:tmp = sdd[sdd['name'] == 'song']
tmp[0]['age'] = 41
sdd[sdd['name'] == 'song'] = tmp
请注意,如果您想一次更新名为song的所有人,而不是只更新第一个(希望只更新?)名为song的人,您只需删除[0],相同的代码就可以工作了。在
但是,如果您确实确定只有一个song,并且只想更新他,那么您可以在xnx's answer中使用基于where的解决方案,这将有效地将包含掩码索引数组的多个索引转换为一个简单的索引。在