arrays - Python inserting a row in a 2Darray -
i have 5x17511 2d array (name = 'da') made pandas.read_csv(...)
i added 1 column indexing this: da.index = pd.date_range(...)
so 2d array has 6x17511 size now.
i'd insert/append 1 more row 2d array, how this?
i tried with: np.insert(da,1,np.array((1,2,3,4,5,6)), 0)
says:
valueerror: shape of passed values (6, 17512), indices imply (6, 17511)
thanks in advance!
i have assumed numpy question rather pandas question ...
you try vstack ...
import numpy np da = np.random.rand(17511, 6) newrow = np.array((1,2,3,4,5,6)) da = np.vstack([da, newrow])
which yields ...
in [5]: da out[5]: array([[ 0.50203777, 0.55102172, 0.74798053, 0.57291239, 0.38977322, 0.40878739], [ 0.9960413 , 0.22293403, 0.34136638, 0.12845067, 0.20262593, 0.50798698], [ 0.05298782, 0.09129754, 0.40833606, 0.67150583, 0.19569471, 0.75176924], ..., [ 0.97927055, 0.44649323, 0.84851791, 0.05370892, 0.94375771, 0.24508979], [ 0.85952039, 0.2852414 , 0.85662827, 0.97665465, 0.65528357, 0.71483845], [ 1. , 2. , 3. , 4. , 5. , 6. ]]) in [6]: len(da) out[6]: 17512
and (albeit different random numbers), can access top , bottom of numpy array follows ...
in [9]: da[:5] out[9]: array([[ 0.76697236, 0.96475768, 0.09145486, 0.27159858, 0.05160006, 0.66495098], [ 0.62635043, 0.1316334 , 0.66257157, 0.99141318, 0.77212699, 0.17016979], [ 0.86705298, 0.11120927, 0.29585339, 0.44128326, 0.32290492, 0.99298705], [ 0.74053894, 0.90743885, 0.99838398, 0.40713677, 0.17337202, 0.56982539], [ 0.99136919, 0.13045787, 0.67881652, 0.03814385, 0.98036307, 0.53594215]]) in [10]: da[-5:] out[10]: array([[ 0.8793664 , 0.0392912 , 0.8106504 , 0.17920025, 0.26767578, 0.98386519], [ 0.41231276, 0.02633723, 0.7872108 , 0.60894162, 0.5358851 , 0.65758067], [ 0.10341791, 0.48079533, 0.1638601 , 0.5470736 , 0.7339205 , 0.60609949], [ 0.55320512, 0.12962241, 0.84443947, 0.81012583, 0.22057856, 0.33495709], [ 1. , 2. , 3. , 4. , 5. , 6. ]])
Comments
Post a Comment