numpy:替换重新数组中的值

发布于 2024-11-14 07:30:32 字数 471 浏览 3 评论 0原文

我对 numpy 很陌生,我正在尝试替换重新数组中的值。所以我有这个数组:

import numpy as np
d = [('1', ''),('4', '5'),('7', '8')]
a = np.array(d, dtype=[('first', 'a5'), ('second', 'a5')])

我想做这样的事情:

ind = a=='' #Replace all blanks
a[ind] = '12345'

但这不能正常工作。我能够做到这一点:

col = a['second']
ind = col=='' #Replace all blanks
col[ind] = '54321'
a['second'] = col

这有效,但我宁愿有一种方法可以在整个重新排列中做到这一点。有人有更好的解决方案吗?

I'm pretty new to numpy, and I'm trying to replace a value in a recarray. So I have this array:

import numpy as np
d = [('1', ''),('4', '5'),('7', '8')]
a = np.array(d, dtype=[('first', 'a5'), ('second', 'a5')])

I would like to do something like this:

ind = a=='' #Replace all blanks
a[ind] = '12345'

but that doesnt work properly. I was able to do this:

col = a['second']
ind = col=='' #Replace all blanks
col[ind] = '54321'
a['second'] = col

Which works, but I would rather have a way to do it over the entire recarray. Anyone have a better solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘你却要生生世世 2024-11-21 07:30:32

据我所知,numpy 的“逐元素”操作(使用它您可以一次对数组的所有元素执行某些函数而无需循环)不适用于重新数组。您只能对各个列执行此操作。

如果你想使用重新排列,我认为最简单的解决方案是循环不同的列,虽然你想要另一个解决方案,但你可以像这样自动完成:

for fieldname in a.dtype.names:
    ind = a[fieldname] == ''
    a[fieldname][ind] = '54321'

但也许你应该考虑是否真的需要重新排列,并且不能只需使用普通的 ndarray 即可。当然,如果您只有一种数据类型(如示例中所示),那么唯一的优点是列名。

The "element-by-element" operations of numpy (with wich you can perform some function on all elements of the array at once without a loop) don't work with recarrays as far as I know. You can only do that with the individual columns.

If you want to use recarrays, I think the easiest solution is to loop the different columns, although you wanted another solution, but you can do it pretty automatic like this:

for fieldname in a.dtype.names:
    ind = a[fieldname] == ''
    a[fieldname][ind] = '54321'

But maybe you should consider if you really need recarrays, and can't just use normal ndarray. Certainly if you have only one data type (as in the example), then the only advantage are the column names.

巷雨优美回忆 2024-11-21 07:30:32

一种可能的解决方案:

a[np.where(a['second']=='')[0][0]]['second']='12345'

One possible solution:

a[np.where(a['second']=='')[0][0]]['second']='12345'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文