如果我有一个看起来像ar的数组,这是一个numpy数组?如何使用numpy mask替换2012年的所有“replaceme”。
ar = [['replaceme', 1,2], ['replaceme', 3,4],['replaceme', 5,6]]
ar = [[2012, 1,2], [2012, 3,4],[2012, 5,6]]
答案 0 :(得分:4)
这不是面具的工作,而是advanced indexing:
>>> import numpy as np
>>> ar = np.array([['replaceme', 1,2], ['replaceme', 3,4],['replaceme', 5,6]], dtype=object)
>>> ar
array([[replaceme, 1, 2],
[replaceme, 3, 4],
[replaceme, 5, 6]], dtype=object)
>>> ar == "replaceme"
array([[ True, False, False],
[ True, False, False],
[ True, False, False]], dtype=bool)
所以我们需要做的就是:
>>> ar[ar == "replaceme"] = 2012
>>> ar
array([[2012, 1, 2],
[2012, 3, 4],
[2012, 5, 6]], dtype=object)