我正在使用numpy和pandas尝试将许多异构值连接成一个单一的数组。
以下是精确的数值:
然而,有效的方法是嵌套追加和连接。
尽管这有点混乱。有人有更好的解决方案来连接多个异构数组吗?
np.concatenate((tmp, id, freqs))
以下是精确的数值:
tmp = np.array([u'DNMT3A', u'p.M880V', u'chr2', 25457249], dtype=object)
freqs = np.array([0.022831050228310501], dtype=object)
id = "id_23728"
tmp
、17232
和freqs
的维度如下:
[in] tmp.shape
[out] (4,)
[in] np.array(17232).shape
[out] ()
[in] freqs.shape
[out] (1,)
我还尝试将它们全部转换为numpy数组,但没有成功。
虽然变量freqs
通常会有多个值。
然而,使用np.concatenate
和np.append
函数都会出现以下错误:
*** ValueError: all the input arrays must have same number of dimensions
这些都具有相同数量的列(0)
,为什么我不能使用上述任一numpy方法将它们连接起来?
我要获取的只是一个单一维度数组[(tmp), 17232, (freqs)]
,它将附加到pandas dataframe的末尾。
谢谢。
更新
看起来我可以将这两个现有数组连接起来:
np.concatenate([tmp, freqs],axis=0)
array([u'DNMT3A', u'p.M880V', u'chr2', 25457249, 0.022831050228310501], dtype=object)
然而,即使进行强制类型转换,整数也不能用于连接字符串。
np.concatenate([tmp, np.array(17571)],axis=0)
*** ValueError: all the input arrays must have same number of dimensions
然而,有效的方法是嵌套追加和连接。
np.concatenate((np.append(tmp, 17571), freqs),)
array([u'DNMT3A', u'p.M880V', u'chr2', 25457249, 17571,
0.022831050228310501], dtype=object)
尽管这有点混乱。有人有更好的解决方案来连接多个异构数组吗?
np.concatenate(map(np.atleast_1d, [tmp, id, freqs]))
- askewchan