在NumPy中使用SQL join或R的merge()函数? [英] SQL join or R's merge() function in NumPy?

查看:259
本文介绍了在NumPy中使用SQL join或R的merge()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种实现可以根据它们的键将两个数组连接在一起?说到哪一种,是将密钥存储在NumPy列之一中的规范方法(NumPy没有'id'或'rownames'属性)?

Is there an implementation where I can join two arrays based on their keys? Speaking of which, is the canonical way to store keys in one of the NumPy columns (NumPy doesn't have an 'id' or 'rownames' attribute)?

推荐答案

如果只想使用numpy,则可以使用结构化数组lib.recfunctions.join_by函数(请参见http://pyopengl.sourceforge.net/pydoc/numpy.lib.recfunctions.html ).一个小例子:

If you want to use only numpy, you can use structured arrays and the lib.recfunctions.join_by function (see http://pyopengl.sourceforge.net/pydoc/numpy.lib.recfunctions.html). A little example:

In [1]: import numpy as np
   ...: import numpy.lib.recfunctions as rfn
   ...: a = np.array([(1, 10.), (2, 20.), (3, 30.)], dtype=[('id', int), ('A', float)])
   ...: b = np.array([(2, 200.), (3, 300.), (4, 400.)], dtype=[('id', int), ('B', float)])

In [2]: rfn.join_by('id', a, b, jointype='inner', usemask=False)
Out[2]: 
array([(2, 20.0, 200.0), (3, 30.0, 300.0)], 
      dtype=[('id', '<i4'), ('A', '<f8'), ('B', '<f8')])

另一种选择是使用 pandas (文档).我没有经验,但是它提供了比标准numpy更强大的数据结构和功能,使处理关系"或标签"数据既简单又直观".而且它当然具有联接和合并功能(例如,请参见 http://pandas.sourceforge.net/merging.html#joining-on-a-key ).

Another option is to use pandas (documentation). I have no experience with it, but it provides more powerful data structures and functionality than standard numpy, "to make working with "relational" or "labeled" data both easy and intuitive". And it certainly has joining and merging functions (for example see http://pandas.sourceforge.net/merging.html#joining-on-a-key).

这篇关于在NumPy中使用SQL join或R的merge()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆