我正在使用numpy和pandas嘗試將多個異值連接成單個數組。numpy arrays尺寸不匹配
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)]
,它將被附加到熊貓數據框的末尾。
謝謝。
更新
看來我可以連接現有的兩個數組:
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)
雖然這是一種混亂。有沒有人有更好的解決方案來連接多個異構陣列?
不是很好,但至少它不關心哪些東西是0d:'np.concatenate(map(np.atleast_1d,[tmp,id,freqs]))' – askewchan
@askewchan很好,atleast_1d函數沒有越過我的雷達。謝謝 – lennart